类型:高精度加法
总结几个较舒服的做法:
1。输入字符串,在转换成整数的同时把串反转, 这样便于计算。
如输入:1234
存储:a[0]=4, a[1]=3, .....
2。把字符串长度存在首个元素里,如a[0]。
3。从MAX递减寻找非0数,开始输出,不用记录结果长度。
这题wa了n次,只因为数组开小了,真不知道数据不符合题意也往上搬,害得人浪费时间。所以以后实在不行,可以试试是不是MAX设小了
#
include<stdio.h>
# include<string.h>
# define MAX 104
char a[ MAX ][ MAX ];
char s[ MAX ];
int main()
{
int i , j , num , maxLen , carry;
for (i = 0 ; scanf( " %s " , s) && ! (s[ 0 ] == ' 0 ' && s[ 1 ] == 0 ); i ++ )
{
a[i][ 0 ] = strlen (s);
for (j = 1 ; j <= a[i][ 0 ]; j ++ )
a[i][j] = s[a[i][ 0 ] - j] - ' 0 ' ;
}
num = i;
for (i = 1 ; i < num; i ++ )
{
carry = 0 ;
for (j = 1 ; j <= MAX - 2 ; j ++ )
{
a[ 0 ][j] += carry + a[i][j];
carry = a[ 0 ][j] / 10 ;
a[ 0 ][j] %= 10 ;
}
}
i = MAX - 1 ;
while (a[ 0 ][i] == 0 )
i -- ;
for (; i >= 1 ; i -- )
printf ( " %d " , a[ 0 ][i]);
printf ( "/n " );
return 0 ;
}
# include<string.h>
# define MAX 104
char a[ MAX ][ MAX ];
char s[ MAX ];
int main()
{
int i , j , num , maxLen , carry;
for (i = 0 ; scanf( " %s " , s) && ! (s[ 0 ] == ' 0 ' && s[ 1 ] == 0 ); i ++ )
{
a[i][ 0 ] = strlen (s);
for (j = 1 ; j <= a[i][ 0 ]; j ++ )
a[i][j] = s[a[i][ 0 ] - j] - ' 0 ' ;
}
num = i;
for (i = 1 ; i < num; i ++ )
{
carry = 0 ;
for (j = 1 ; j <= MAX - 2 ; j ++ )
{
a[ 0 ][j] += carry + a[i][j];
carry = a[ 0 ][j] / 10 ;
a[ 0 ][j] %= 10 ;
}
}
i = MAX - 1 ;
while (a[ 0 ][i] == 0 )
i -- ;
for (; i >= 1 ; i -- )
printf ( " %d " , a[ 0 ][i]);
printf ( "/n " );
return 0 ;
}