zoukankan      html  css  js  c++  java
  • 正式定居博客圆,发些以前在Topcoder上的练习题,对算法和STL有兴趣的朋友可以看下:)

    首先介绍下TopCoder网站(http://www.topcoder.com/tc),这个网站是全球编程爱好者的积聚地,无论是对算法或是工程开发感兴趣的朋友都可以来这里获取你感兴趣的信息这里最有名的当属它的Competitions Arena,这是个用Applet做的很酷的编程竞技平台,与国内的ACM算法平台(PKU Judge online,zju等)相比,我更喜欢前者:)你需要安装了JDK1.4X以上的版本才可以运行该程序,如果你机子没装JDK,当你点击进入后,只要按提示一步步操作即可完成从下载到安装的全过程.

    TopCoder使用三种语言作为编程语言:C++(STL),C#,Java,你完全可以选择你熟悉的语言开始你的竞技.我是在去年年底参加Google Code Jam时才知道了这个网站,之前只在PKU Judge online(http://acm.pku.edu.cn/JudgeOnline/)练习算法,之后就开始常常来了,在这里可以接触到来自全世界各地的高手,呵呵.
    好了,废话了这么多,开始切入正题吧,以TopCoder上的SRM(Single Round Match)的真题做介绍,每道题附上我的个人解答.大家也可以发表自己的见解,呵呵.

    题目来源:SRM250 DIV2
    级别:250Points
    题目:
    Problem Statement
        
    An electronics manufacturer has called you in to make a program to decode resistor color codes. You are given a vector <string> code containing three elements corresponding to the first three color bands on a resistor. Return the # of Ohms the resistor represents.  The first two bands of resistors represent the value, while the third is a multiplier, as shown in the following chart:
    Color:      Value:       Multiplier:

    black         0                   1
    brown         1                  10
    red           2                 100
    orange        3               1,000
    yellow        4              10,000
    green         5             100,000
    blue          6           1,000,000
    violet        7          10,000,000
    grey          8         100,000,000
    white         9       1,000,000,000
     For example if you are given { "yellow", "violet", "red" }, you would return 4700.
    Definition
        
    Class:
    ColorCode
    Method:
    getOhms
    Parameters:
    vector <string>
    Returns:
    long long
    Method signature:
    long long getOhms(vector <string> code)
    (be sure your method is public)
        

    Notes
    -
    Actual resistors can have a 4th and even a 5th band representing the tolerance, and the amount the value might change in 1,000 hours of use, respectively, but for our purposes we will only deal with the first three bands.
    Constraints
    -
    code consists of 3 elements each containing one of the color words above, all in lower case.
    Examples
    0)

        
    { "yellow", "violet", "red" }
    Returns: 4700
    The example from the problem statement.
    1)

        
    { "orange", "red", "blue" }
    Returns: 32000000

    2)

        
    { "white", "white", "white" }
    Returns: 99000000000
    The maximum possible.

    /*这道题是这个SRM里分值最低的题,也就是说,应该是最简单的题,这里说一下做题的技巧,通常很多人都喜欢从低分题做起,但实际上,如果你想晋级的话,选更高分的题来做(确保做对),也许会更稳妥,低分题只能拼速度,毕竟只有1个小时,做完低分题也就很难有时间再做高分的了,如果有人做出了高分题,哪怕他花了59分钟,那么他得到的实际分数依然将比你高.好,言归正传,本题其实非常简单,就是求个电阻值,按颜色码对应不同的值,前两个为实际的电阻值,第三个是乘法器,返回的结果就是电阻值*乘法器.我的代码如下*/

    #include <iostream>
    #include 
    <string>
    #include 
    <stdio.h>
    #include 
    <vector>
    #include 
    <set>
    #include 
    <map>
    #include 
    <algorithm>

    using namespace std;

    enum ECOLOR
    {
        black,
        brown,
        red,
        orange,
        yellow,
        green,
        blue,
        violet,
        grey,
        white
    }
    ;

    const long long multier[10= {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};

    class ColorCode
    {
    public:
        ECOLOR color;
        
    long long curOhms;
        
    public:

        
    long long getOhms(vector <string> code)
        
    {
            curOhms 
    = 0;
            
    for(int i=0;i<code.size();i++)
            
    {
                
    if(i==0)
                    curOhms 
    = 10*GetColorValue(code[0]);
                
    else if(i==1)
                    curOhms
    += GetColorValue(code[1]);
                
    else curOhms*= multier[GetColorValue(code[2])];
            }

            
    return curOhms;
        }


        
    int GetColorValue(string cstr)
        
    {
            
    if(cstr[0]=='b')
            
    {
                
    if(cstr.compare("black")==0)
                
    return 0;
                
    else if(cstr.compare("brown")==0)
                
    return 1;
                
    else if(cstr.compare("blue")==0)
                
    return 6;

            }

            
    else if(cstr[0]=='g')
            
    {
                
    if(cstr.compare("green")==0)
                
    return 5;
                
    else if(cstr.compare("grey")==0)
                
    return 8;
            }

            
    else if(cstr.compare("red")==0)
                
    return 2;
            
    else if(cstr.compare("orange")==0)
                
    return 3;
            
    else if(cstr.compare("yellow")==0)
                
    return 4;
            
    else if(cstr.compare("violet")==0)
                
    return 7;
            
    else if(cstr.compare("white")==0)
                
    return 9;
            
    else return -1;

        }

    }
    ;

    非常简单,我完成此题大概只花了10几分钟,相信你能比我更快:)
    下一篇,我将继续介绍这个SRM的500points的题:)
  • 相关阅读:
    DB-MySQL:MySQL 正则表达式
    DB-MySQL:MySQL 事务
    DB-MySQL:MySQL 索引
    DB-MySQL:MySQL 临时表
    DB-MySQL:MySQL 复制表
    DB-MySQL:MySQL 序列使用
    DB-MySQL:MySQL 处理重复数据
    DB-MySql:MySQL 及 SQL 注入
    mysql
    PHP+jQuery 注册模块的改进之一:验证码存入SESSION
  • 原文地址:https://www.cnblogs.com/wuxilin/p/361316.html
Copyright © 2011-2022 走看看