问题:
http://poj.org/problem?id=3508思路:
挺简单的算数题(注意进位),简化为:
abc
+ abc
---------
353
第一次提交居然TLE,汗...
然后把memset(result, 0, sizeof(result))的代码删掉,再适当减少些运算,就AC了,860MS
代码:
1 /* 860MS */
2 #include<stdio.h>
3 #include<stdlib.h>
4 #include<string.h>
5 #define MAX_LEN 1000003
6 char num[MAX_LEN], result[MAX_LEN];
7 int len;
8
9 void
10 solve()
11 {
12 int i, mark, minus, tmp;
13 minus = mark = 0;
14 for(i=len-1; i>=0; i--) {
15 minus += mark;
16 if(minus <= (num[i]-'0')) {
17 result[i] = num[i] - minus;
18 mark = 0;
19 } else {
20 result[i] = num[i] + 10 - minus;
21 mark = 1;
22 }
23 minus = result[i]-'0';
24 }
25 result[len] = '\0';
26 if(result[0] == '0')
27 printf("IMPOSSIBLE\n");
28 else
29 printf("%s\n", result);
30 }
31
32 int
33 main(int argc, char **argv)
34 {
35 int tests = 0;
36 while(scanf("%s", num)!=EOF && num[0]!='0') {
37 len = strlen(num);
38 printf("%d. ", ++tests);
39 solve();
40 }
41 }