這是我的code:
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char a[11], b[11], c[11] = "0000000000";
int i, ans = 0, la, lb;
while (scanf ("%s %s", &a, &b) != EOF){
if ( (a[0] == '0') && (b[0] == '0') ){
break;
}
la = strlen (a);
lb = strlen (b);
for (i = la - 1 ; i >= 0 ; i--){
c[la - i - 1] = a[i];
//printf ("c = \"%s\"\n", c);
}
for (i = lb - 1 ; i >= 0 ; i--){
c[lb - i - 1] += b[i] - '0';
//printf ("c = \"%s\"\n", c);
}
for (i = 0 ; i < 10 ; i++){
if (c[i] > '9'){
c[i + 1] += (c[i] - '9');
ans++;
//printf ("c = \"%s\"\n", c);
}
}
if ( ! ans){
puts ("No carry operation.");
}
else if (ans == 1){
puts ("1 carry operation.");
}
else {
printf("%d", ans);
puts (" carry operations.");
}
//Clearing the char array a, b, c and the int ans......
for (i = 0 ; i < 10 ; i++){
a[i] = '0';
b[i] = '0';
c[i] = '0';
}
ans = 0;
}
return 0;
}
我試過(1,999),(88,12),(494,494),(1,0),(0,1)都沒錯
可是不知為何 ...
WA (line:2)
您的答案為: 3 carry operations. 正確答案為: 1 carry operation.
請各位大大幫小弟看看......
不好意思
我需要的是陣列解法
麻煩不要給我高精度運算......
謝謝喔!!!
for (i = 0 ; i < 10 ; i++){
if (c[i] > '9'){
c[i + 1] += (c[i] - '9');
ans++;
//printf ("c = \"%s\"\n", c);
}
}