TopCoder竞赛题基础_第1页
TopCoder竞赛题基础_第2页
TopCoder竞赛题基础_第3页
TopCoder竞赛题基础_第4页
TopCoder竞赛题基础_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 se

2、conds 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 , , an

3、d should be an integer, 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 betw

4、een 0 and 24*60*60 - 1 = 86399, inclusive. 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

5、; int m = seconds/60%60; int s = seconds%60; char str64; sprintf(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

6、 is about the scoring in the first phase of the die-game yahtzee, where five dice are 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 val

7、ues on active dice. say, for instance, that a player ends up with the die faces showing 2, 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

8、 a int toss, where each element represents the upward face of a die, and return the maximum possible score 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 con

9、tain exactly 5 elements. each element of toss will be between 1 and 6, inclusive. examples 0) 2, 2, 3, 5, 4 returns: 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 maxi

10、mum number of points. 2) 5, 3, 5, 3, 3 returns: 10 source code #include #include using namespace std; class yahtzeescore public: int maxpoints(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; ; vo

11、id 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.push_back(3); int ret = ya-maxpoints(v); coutretendl; 3) srm 147 div 2, 250-point problem statement julius caesar used a system o

12、f 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 around, that is y shifts to a. we can, of course, try shifting by any number. given an encoded text and a number o

13、f 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 example 0 below. definition class: ccipher method: decode parameters: string, int returns: string

14、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-z 5 shift is between 0 and 25 inclusive examples 0) vqreqfgt 2 returns: topcoder 1) abcd

15、efghijklmnopqrstuvwxyz 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 namespace std; class ccipher public: string decode(string ciphertext, int s

16、hift) 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.begin(), str.end(), output); coutendl; r

17、eturn 0; 4) srm 148 div2, 250-point problem statement create a class divisordigits containing a method howmany which takes an int number and returns how many digits in number divide evenly into number itself. definition class: divisordigits method: howmany parameters: int returns: int method signatu

18、re: int howmany(int number) (be sure your method is public) notes no number is divisible by 0. constraints number will be between 10000 and 999999999. examples 0) 12345 returns: 3 7 12345 is divisible by 1, 3, and 5. 1) 661232 returns: 2 661232 is divisible by 1 and 2 (doubled). 2) 52527 returns: 0

19、52527 is not divisible by 5, 2, or 7. 3) 730000000 returns: 0 nothing is divisible by 0. in this case, the number is also not divisible by 7 or 3. sourece code #include using namespace std; class divisordigits public: int howmany(int number) int count = 0; int x = number; while(x) int y = x%10; x/=1

20、0; if(0 = y) continue; if(0 = number%y) count+; return count; ; int main(void) divisordigits dd; / int cnt = dd.howmany(12345); / int cnt = dd.howmany(730000000); 8 int cnt = dd.howmany(661232); coutcntendl; return 0; 5) srm 149 div2, 250-point problem statement in documents, it is frequently necess

21、ary to write monetary amounts in a standard format. we have decided to format amounts as follows: the amount must start with $ the amount should have a leading 0 if and only if it is less then 1 dollar. the amount must end with a decimal point and exactly 2 following digits. the digits to the left o

22、f the decimal point must be separated into groups of three by commas (a group of one or two digits may appear on the left). create a class formatamt that contains a method amount that takes two ints, dollars and cents, as inputs and returns the properly formatted string. definition class: formatamt

23、method: amount parameters: int, int returns: string method signature: string amount(int dollars, int cents) (be sure your method is public) notes one dollar is equal to 100 cents. constraints dollars will be between 0 and 2,000,000,000 inclusive cents will be between 0 and 99 inclusive examples 0) 1

24、23456 0 returns: $123,456.00 note that there is no space between the $ and the first digit. 1) 49734321 9 returns: $49,734,321.09 9 2) 0 99 returns: $0.99 note the leading 0. 3) 249 30 returns: $249.30 4) 1000 1 returns: $1,000.01 source code #include #include /#include /#include /#include /#include

25、 /#include /#include /#include /#include /#include #include /#include /#include /#include /#include /#include using namespace std; class formatamt public: string amount(int, int); ; string formatamt:amount(int dollars, int cents) 10 string str = ; int n = 0; int a100; while(dollars 0) an+ = dollars%

26、10; dollars /= 10; int count = 0; for(int i=0; i 0) an+ = cents % 10; cents /= 10; for(i=1; i=0; i-) str += char(ai + 48); return str; int main(void) formatamt amt; coutamt.amount(123456,0)endl; coutamt.amount(49734321,9)endl; coutamt.amount(0,99)endl; coutamt.amount(100,1)endl; return 0; 11 6) srm

27、150 div 2, 250-point problem statement when a widget breaks, it is sent to the widget repair shop, which is capable of repairing at most numperday widgets per day. given a record of the number of widgets that arrive at the shop each morning, your task is to determine how many days the shop must oper

28、ate to repair all the widgets, not counting any days the shop spends entirely idle. for example, suppose the shop is capable of repairing at most 8 widgets per day, and over a stretch of 5 days, it receives 10, 0, 0, 4, and 20 widgets, respectively. the shop would operate on days 1 and 2, sit idle o

29、n day 3, and operate again on days 4 through 7. in total, the shop would operate for 6 days to repair all the widgets. create a class widgetrepairs containing a method days that takes a sequence of arrival counts arrivals (of type vector ) and an int numperday, and calculates the number of days of operation. definition class: widgetrepairs method: days parameters: vector , int returns: int method signature: int days(vector arrivals, int numperday) (be sure your method is public) constraints arrivals contains betwee

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论