杭电oj 1002
本人测试很多数都没问题,提交上去一直是WA,求高手看一下问题在哪
本人测试很多数都没问题,提交上去一直是WA,求高手看一下问题在哪
#include <stdio.h> void cti(char s1[]) // 将char值转换成int值 { int i,len = strlen(s1); for ( i = 0; i < len; ++i) { s1[i] = s1[i] - ""0""; } } int add(char s1[],char s2[], int sum[]) // 将两个输入加到输出中 { int i,max,temp,j; int len1 = strlen(s1),len2 = strlen(s2); max = len1 > len2 ? len1 : len2; temp = 0; for (i = 0;i < max; ++i) { if (i < len1 && i < len2) { sum[i] = (s1[len1-1-i] + s2[len2-1-i] + temp) % 10; temp = (s1[len1-1-i] + s2[len2-1-i] + temp) / 10; } else if (i < len1) { sum[i] = (s1[len1-1-i] + temp) % 10; temp = (s1[len1-1-i] + temp) / 10; } else { sum[i] = (s2[len2-1-i] + temp) % 10; temp = (s2[len2-1-i] + temp) / 10; } } if (temp) { sum[i] = temp; return max + 1; } return max; } void itc(int arr[], int len) // 将int值转换回char值 { int i; for (i = 0; i < len; ++i) { arr[i] += ""0""; } } int main() { char s1[1000],s2[1000]; int sum[1001],num,i,length,j; for (i = 0; i < 1000; ++i) { // 初始化 s1[i] = s2[i] = ""\0""; sum[i] = 0; } sum[i] = 0; scanf("%d",&num); for (i = 1; i <= num; ++i) { scanf("%s %s",s1,s2); printf("Case %d:\n%s + %s = ",i,s1,s2); cti(s1); // 转换char值 cti(s2); // 转换char值 length = add(s1,s2,sum); itc(sum,length); // 转换int值 for (j = length - 1; j >= 0; --j) { // 倒序输出字符串结果 printf("%c",sum[j]); } printf("\n\n"); } // end for }
解决方案:20分
LZ程序主要有2个错误:
1. 不能正确计算有前导0的数据,例如:001 + 2。
2. 最后一行输出后多了空行。
如下修改可AC:
1. 不能正确计算有前导0的数据,例如:001 + 2。
2. 最后一行输出后多了空行。
如下修改可AC:
#include <stdio.h> #include <string.h> //加 void cti(char s1[]) // 将char值转换成int值 { int i,len = strlen(s1); for ( i = 0; i < len; ++i) { s1[i] = s1[i] - ""0""; } } int add(char s1[],char s2[], int sum[]) // 将两个输入加到输出中 { int i,max,temp,j; int len1 = strlen(s1),len2 = strlen(s2); max = len1 > len2 ? len1 : len2; cti(s1);//加 cti(s2);//加 temp = 0; for (i = 0;i < max; ++i) { if (i < len1 && i < len2) { sum[i] = (s1[len1-1-i] + s2[len2-1-i] + temp) % 10; temp = (s1[len1-1-i] + s2[len2-1-i] + temp) / 10; } else if (i < len1) { sum[i] = (s1[len1-1-i] + temp) % 10; temp = (s1[len1-1-i] + temp) / 10; } else { sum[i] = (s2[len2-1-i] + temp) % 10; temp = (s2[len2-1-i] + temp) / 10; } } if (temp) { sum[i] = temp; return max + 1; } return max; } void itc(int arr[], int len) // 将int值转换回char值 { int i; for (i = 0; i < len; ++i) { arr[i] += ""0""; } } int main() { char s1[1001],s2[1001];//改 char s1[1000],s2[1000]; int sum[1001],num,i,length,j; for (i = 0; i < 1000; ++i) { // 初始化 s1[i] = s2[i] = ""\0""; sum[i] = 0; } sum[i] = 0; scanf("%d",&num); for (i = 1; i <= num; ++i) { scanf("%s %s",s1,s2); printf("Case %d:\n%s + %s = ",i,s1,s2); //移到add中 cti(s1); // 转换char值 //移到add中 cti(s2); // 转换char值 length = add(s1,s2,sum); itc(sum,length); // 转换int值 for (j = length - 1; j >= 0; --j) { // 倒序输出字符串结果 printf("%c",sum[j]); } printf("\n"); //改 printf("\n\n"); if (i < num) printf("\n"); //加 } // end for return 0; //加 }
解决方案:20分
仅供参考:
#include <stdio.h> #include <string.h> #define MAXLEN 1000 char a1[MAXLEN]; char a2[MAXLEN]; static int v1[MAXLEN]; static int v2[MAXLEN]; static int v3[MAXLEN]; int i,j,n,L,z; void main(void) { scanf("%d",&n); for (j=0;j<n;j++) { scanf("%s%s",a1,a2); L=strlen(a1); for (i=0;i<L;i++) v1[i]=a1[L-1-i]-""0""; L=strlen(a2); for (i=0;i<L;i++) v2[i]=a2[L-1-i]-""0""; for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i]; for (i=0;i<MAXLEN;i++) { if (v3[i]>=10) { v3[i+1]+=v3[i]/10; v3[i]=v3[i]%10; } } printf("Case %d:\n", j+1); printf("%s + %s = ", a1, a2); z=0; for (i=MAXLEN-1;i>=0;i--) { if (z==0) { if (v3[i]!=0) { printf("%d",v3[i]); z=1; } } else { printf("%d",v3[i]); } } if (z==0) printf("0"); printf("\n"); } } //Sample Input //3 //0 0 //1 2 //112233445566778899 998877665544332211 // //Sample Output //Case 1: //0 + 0 = 0 //Case 2: //1 + 2 = 3 //Case 3: //112233445566778899 + 998877665544332211 = 1111111111111111110