PART1:A+B
PROBLEM:
Input A and B, output A+B
Input
Input two values, A and B.
Output
Output the result of A+B.
[warning]
题目没有说 A 和 B 的范围,注意溢出的处理,A 和 B 都是正整数,特别不要写 cout<<A+B 这样的语句,C++并没有想象中聪明。
另外注意,最后一行要输出一个回车换行符 n。
[/warning]
PART2:Alphacode
Problem
Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages: Alice: "Let’s just use a very simple code: We’ll assign `A’ the code word 1, `B’ will be 2, and so on down to `Z’ being assigned 26." Bob: "That’s a stupid code, Alice. Suppose I send you the word `BEAN’ encoded as 25114. You could decode that in many different ways!" Alice: "Sure you could, but what words would you get? Other than `BEAN’, you’d get `BEAAD’, `YAAD’, `YAN’, `YKD’ and `BEKD’. I think you would be able to figure out the correct decoding. And why would you send me the word `BEAN’ anyway?" Bob: "OK, maybe that’s a bad example, but I bet you that if you got a string of length 500 there would be tons of different decodings and with that many you would find at least two different ones that would make sense." Alice: "How many different decodings?" Bob: "Jillions!" For some reason, Alice is still unconvinced by Bob’s argument, so she requires a program that will determine how many decodings there can be for a given string using her code.
Input
Input will consist of multiple input sets. Each set will consist of a single line of digits representing a valid encryption (for example, no line will begin with a 0). There will be no spaces between the digits. An input line of `0′ will terminate the input and should not be processed
Output
For each input set, output the number of possible decodings for the input string. All answers will be within the range of a long variable.
Sample Input
25114
1111111111
3333333333
0
Sample Output
6
89
1
[tip]
用字符串数组 str 记录输入,用数组 m[i] 记录字符串 str[i..end] 可能的编码数,从后面往前面推,一直推到 m[0],则 m[0] 记录的就是字符串可能的编码数。
[/tip]
PART1 的代码从略,以下是 PART2 的关键部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //a=0,b=strlen(str)-1 void lookup(int a, int b){ m[b + 1] = 1;//保持循环不变式性质的初始化 for (int i = b; i >= 0; i--){ if (str[i] == '0'){//等于0时 m[i] = 0; continue; } if (i == b)//如果是最后一位时 m[i] = 1; else{//否则 m[i] = 0; m[i] += m[i + 1]; int kk = (str[i] - '0') *10+str[i + 1] - '0'; if (kk >= 10 && kk <= 26){ m[i] += m[i + 2]; } } } } |
一堆 ACM,看到我热血沸腾
我也是刚入门而已… 弄些入门的题大家 share 一下