正式定居博客圆,发些以前在Topcoder上的练习题,对算法和STL有兴趣的朋友可以看下:)...

本文介绍TopCoder网站及其编程竞技平台,特别解析了SRM250DIV2级别250Points题目的解题思路及代码实现,涉及电阻颜色码转换为欧姆值的问题。

首先介绍下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分钟,那么他得到的实际分数依然将比你高.好,言归正传,本题其实非常简单,就是求个电阻值,按颜色码对应不同的值,前两个为实际的电阻值,第三个是乘法器,返回的结果就是电阻值*乘法器.我的代码如下*/

 

None.gif #include  < iostream >
None.gif#include 
< string >
None.gif#include 
< stdio.h >
None.gif#include 
< vector >
None.gif#include 
< set >
None.gif#include 
< map >
None.gif#include 
< algorithm >
None.gif
None.gif
using   namespace  std;
None.gif
None.gif
enum  ECOLOR
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    black,
InBlock.gif    brown,
InBlock.gif    red,
InBlock.gif    orange,
InBlock.gif    yellow,
InBlock.gif    green,
InBlock.gif    blue,
InBlock.gif    violet,
InBlock.gif    grey,
InBlock.gif    white
ExpandedBlockEnd.gif}
;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
const   long   long  multier[ 10 =   dot.gif {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000} ;
None.gif
None.gif
class  ColorCode
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    ECOLOR color;
InBlock.gif    
long long curOhms;
InBlock.gif    
InBlock.gif
public:
InBlock.gif
InBlock.gif    
long long getOhms(vector <string> code)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        curOhms 
= 0;
InBlock.gif        
for(int i=0;i<code.size();i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(i==0)
InBlock.gif                curOhms 
= 10*GetColorValue(code[0]);
InBlock.gif            
else if(i==1)
InBlock.gif                curOhms
+= GetColorValue(code[1]);
InBlock.gif            
else curOhms*= multier[GetColorValue(code[2])];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return curOhms;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
int GetColorValue(string cstr)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(cstr[0]=='b')
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(cstr.compare("black")==0)
InBlock.gif            
return 0;
InBlock.gif            
else if(cstr.compare("brown")==0)
InBlock.gif            
return 1;
InBlock.gif            
else if(cstr.compare("blue")==0)
InBlock.gif            
return 6;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if(cstr[0]=='g')
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(cstr.compare("green")==0)
InBlock.gif            
return 5;
InBlock.gif            
else if(cstr.compare("grey")==0)
InBlock.gif            
return 8;
ExpandedSubBlockEnd.gif        }

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

ExpandedBlockEnd.gif}
;

非常简单,我完成此题大概只花了10几分钟,相信你能比我更快:)
下一篇,我将继续介绍这个SRM的500points的题:)

转载于:https://www.cnblogs.com/wuxilin/archive/2006/03/28/361316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值