TopCoder竞赛题基础.pdf
《TopCoder竞赛题基础.pdf》由会员分享,可在线阅读,更多相关《TopCoder竞赛题基础.pdf(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1 1)SRM 144 DIV 2,250-point Problem Statement Computers tend to store dates and times as single numbers which represent the number of seconds or milliseconds since a particular date.Your task in this problem is to write a method whatTime,which takes an int,seconds,representing the number of seconds
2、since midnight on some day,and returns a String formatted as:.Here,represents the number of complete hours since midnight,represents the number of complete minutes since the last complete hour ended,and represents the number of seconds since the last complete minute ended.Each of,and should be an in
3、teger,with no extra leading 0s.Thus,if seconds is 0,you should return 0:0:0,while if seconds is 3661,you should return 1:1:1.Definition Class:Time Method:whatTime Parameters:int Returns:String Method signature:String whatTime(int seconds)Constraints seconds will be between 0 and 24*60*60-1=86399,inc
4、lusive.Examples 0)0 Returns:0:0:0 1)3661 Returns:1:1:1 2)5436 Returns:1:30:36 3)86399 Returns:23:59:59(be sure your method is public)Source code#include using namespace std;2#include class Time public:string whatTime(int seconds)int h=seconds/3600;int m=seconds/60%60;int s=seconds%60;char str64;spri
5、ntf(str,%d:%d:%d,h,m,s);string ret=str;/coutstrendl;return ret;void main()Time time;/time.whatTime(0);/time.whatTime(3661);/time.whatTime(5436);time.whatTime(86399);2)SRM 146 DIV 2,250-point Problem Statement This task is about the scoring in the first phase of the die-game Yahtzee,where five dice a
6、re used.The score is determined by the values on the upward die faces after a roll.The player gets to choose a value,and all dice that show the chosen value are considered active.The score is simply the sum of values on active dice.Say,for instance,that a player ends up with the die faces showing 2,
7、2,3,5 and 4.Choosing the value two makes the dice showing 2 active and yields a score of 2+2=4,while choosing 5 makes the one die showing 5 active,yielding a score of 5.Your method will take as input a int toss,where each element represents the upward face of a die,and return the maximum possible sc
8、ore with these values.Definition Class:YahtzeeScore Method:maxPoints Parameters:int Returns:int 3 Method signature:int maxPoints(int toss)(be sure your method is public)Constraints toss will contain exactly 5 elements.Each element of toss will be between 1 and 6,inclusive.Examples 0)2,2,3,5,4 Return
9、s:5 The example from the text.1)6,4,1,1,3 Returns:6 Selecting 1 as active yields 1+1=2,selecting 3 yields 3,selecting 4 yields 4 and selecting 6 yields 6,which is the maximum number of points.2)5,3,5,3,3 Returns:10 Source code#include#include using namespace std;class YahtzeeScore public:int maxPoin
10、ts(vector toss)int count6;memset(count,0,sizeof(count);for(int i=0;i5;i+)counttossi-1+=tossi;int max=0;for(i=0;imax)4 max=counti;return max;void main()YahtzeeScore*ya=new YahtzeeScore;vector v;/test array:2 2 3 5 4/6 4 1 1 3/5 3 5 3 3 v.push_back(5);v.push_back(3);v.push_back(5);v.push_back(3);v.pus
11、h_back(3);int ret=ya-maxPoints(v);coutretendl;3)SRM 147 DIV 2,250-point Problem Statement Julius Caesar used a system of cryptography,now known as Caesar Cipher,which shifted each letter 2 places further through the alphabet(e.g.A shifts to C,R shifts to T,etc.).At the end of the alphabet we wrap ar
12、ound,that is Y shifts to A.We can,of course,try shifting by any number.Given an encoded text and a number of places to shift,decode it.For example,TOPCODER shifted by 2 places will be encoded as VQREQFGT.In other words,if given(quotes for clarity)VQREQFGT and 2 as input,you will return TOPCODER.See
13、example 0 below.Definition Class:CCipher Method:decode Parameters:string,int Returns:string Method signature:string decode(string cipherText,int shift)(be sure your method is public)Constraints cipherText has between 0 to 50 characters inclusive each character of cipherText is an uppercase letter A-
14、Z 5 shift is between 0 and 25 inclusive Examples 0)VQREQFGT 2 Returns:TOPCODER 1)ABCDEFGHIJKLMNOPQRSTUVWXYZ 10 Returns:QRSTUVWXYZABCDEFGHIJKLMNOP 2)TOPCODER 0 Returns:TOPCODER 3)ZWBGLZ 25 Returns:AXCHMA 4)DBNPCBQ 1 Returns:CAMOBAP 5)LIPPSASVPH 4 Returns:HELLOWORLD Source code#include#include using n
15、amespace std;class CCipher public:string decode(string cipherText,int shift)string s=;6 for(int i=0;i(int)cipherText.size();i+)char c=cipherTexti;c=(c-A)-shift+26)%26+A;s+=c;return s;int main()ostream_iterator output(cout,);CCipher cipher;string str=VQREQFGT;string Str=cipher.decode(str,2);copy(Str.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TopCoder 竞赛题 基础
限制150内