|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
自己先写了一个,但是在De上面根本就过不了,求大佬用C语言指点
#include<stdio.h>
#include<string.h>
char add(char s1[1010], char s2[1010]){ //大数求和
char addResult[1010]={0};
int result = 0;
int len = 0;
int i = 0;
int len1 = strlen(s1);
int len2 = strlen(s2);
int maxLen = len1 > len2 ? len1 : len2;
int minLen = len1 < len2 ? len1 : len2;
/*printf("%d\n", len);
len = strlen(s1) > strlen(s2) ? strlen(s1) : strlen(s2);
printf("%d\n", len);*/
for(i=maxLen-1;i>maxLen-minLen;i--){
result = (s1[i]+0) + (s2[i]+0);
if(result>9){
addResult[i] = ((result % 10)+'0')+1;
s1[i-1] = s1[i-1]+'1';
}
}
if(result>10){
s1[maxLen-minLen-1] += '1';
}
for(i=maxLen-minLen-1;i>=0;i--){
if((s1[i]+0)>9){
addResult[i] = (s1[i]+0)%10;
s1[i-1] += '1';
}
else{
addResult[i] = s1[i]+0;
}
if((addResult[0]+0)>9){
{
for(i=len;i>1;i--){
addResult[i] = addResult[i-1];
addResult[1] = (addResult[0]+0)%10;
addResult[0] = (addResult[0]+0)/10;
}
}
}
printf("%s\n", addResult);
return 0;
}
int main()
{
char s1[1010]={0};
char s2[1010]={0};
scanf("%s%s", s1, s2);
add(s1, s2);
return 0;
} |
|