HDU ACM 1002题目为:http://acm.hdu.edu.cn/showproblem.php?pid1002我的代码C/C++ code#include iostre
HDU ACM 1002
题目为:http://acm.hdu.edu.cn/showproblem.php?pid=1002
我的代码
- C/C++ code
#include <iostream>#include <string>using namespace std;void func(char * str1, char * str2, char * ans){ if(str1 == NULL || str2 == NULL) return; int len1 = strlen(str1); int len2 = strlen(str2); int i = len1 - 1; int j = len2 - 1; int temp; int c = 0; //c 为进位 if(i > j){ ans[i + 2] = '\0'; while(j >= 0){ temp = (int)(str1[i] - 48) + (int)(str2[j] - 48) + c; c = temp / 10; temp = temp % 10; ans[i + 1] = (char)(temp + 48); i--; j--; } while(i >= 0){ temp = (int)(str1[i] - 48) + c; c = temp / 10; temp = temp % 10; ans[i + 1] = (char)(temp + 48); i--; } if(c == 0){ for(int k = 1; k < i + 2; k++){ ans[k - 1] = ans[k]; } }else{ ans[i + 1] = (char)(c + 48); } }else{ ans[j + 2] = '\0'; while(i >= 0){ temp = (int)(str1[i] - 48) + (int)(str2[j] - 48) + c; c = temp / 10; temp = temp % 10; ans[j + 1] = (char)(temp + 48); i--; j--; } while(j >= 0){ temp = (int)(str2[j] - 48) + c; c = temp / 10; temp = temp % 10; ans[j + 1] = (char)(temp + 48); j--; } if(c == 0){ for(int k = 1; k < j + 2; k++){ ans[k - 1] = ans[k]; } }else{ ans[j + 1] = (char)(c + 48); } }}int main(){ int num; char str1[1000]; char str2[1000]; char ans[1000]; cin >> num; while(num-- > 0){ cin >> str1 >> str2; func(str1, str2, ans); cout << ans << endl; } return 0;}输出结果有错误,我知道网上有更好的方法,请您不要自己做一个更好的方法,我只想知道我的怎么错了?我已经调试过了,但我是菜鸟,没发现错误原因!
[解决办法]
看完楼主的代码,调试了下,发现点问题。
1, 你为什么确定你的最大长度是a或者b中最长字串+2的长度呢?有没有考虑00000000001+00000002那么你结果应该是3,只有一个字符。
[解决办法]
- C/C++ code
if(c == 0){ for(int k = 1; k < j + 2; k++){ ans[k - 1] = ans[k]; } }
[解决办法]
终于彻底的给你修改过了。自己参考下吧。
- C/C++ code
#include <iostream>#include <string>using namespace std;//000001 000000002void func(char * str1, char * str2, char * ans){ if(str1 == NULL || str2 == NULL) return; int len1 = strlen(str1); int len2 = strlen(str2); int i = len1 - 1; int j = len2 - 1; int temp; int c = 0; //c 为进位 if(i > j){ ans[i + 2] = '\0'; while(j >= 0){ temp = (int)(str1[i] - 48) + (int)(str2[j] - 48) + c; c = temp / 10; temp = temp % 10; ans[i + 1] = (char)(temp + 48); i--; j--; } while(i >= 0){ temp = (int)(str1[i] - 48) + c; c = temp / 10; temp = temp % 10; ans[i + 1] = (char)(temp + 48); i--; } if(c == 0) { i = strlen(ans); for(int k = 1; k < i+1; k++){ ans[k - 1] = ans[k]; } } else { ans[i + 1] = (char)(c + 48); } }else{ ans[j + 2] = '\0'; while(i >= 0){ temp = (int)(str1[i] - 48) + (int)(str2[j] - 48) + c; c = temp / 10; temp = temp % 10; ans[j + 1] = (char)(temp + 48); i--; j--; } while(j >= 0){ temp = (int)(str2[j] - 48) + c; c = temp / 10; temp = temp % 10; ans[j + 1] = (char)(temp + 48); j--; } if(c == 0){ j = strlen(ans); for(int k = 1; k < j + 1; k++){ ans[k - 1] = ans[k]; } }else{ ans[j + 1] = (char)(c + 48); } } i = 0; while(ans[i]=='0') i++; j = strlen(ans); for (len1=0;len1<=j-i;len1++) { ans[len1]=ans[len1+i]; }}int main(){ int num,i=0; char str1[1000]; char str2[1000]; char ans[1000]; cin >> num; while(num-- > 0){ i++; cin >> str1 >> str2; memset(ans,48,sizeof(ans)); func(str1, str2, ans); if(i!=1) cout<<endl; cout<<"Case "<<i<<":"<<endl; cout <<str1<<" + "<<str2<<" = "<< ans << endl; } return 0;}
