解题思路:
本题为大数据储存,所以要用数组来储存数据
注意事项:
1.需要考虑是否有进位,特别是最高位,所以需要把储存数字的和的数字多考虑一位
2.需要辅助数组将数字对齐,以便相加。
参考代码:
# include <stdio.h>
# define N 1000
# include <string.h>
int max (int a, int b);
int main(void)
{
char a[N] = {'0'}, b[N] = {'0'};
while (scanf("%s%s", a, b) != EOF)
{
fflush (stdin);
int len_a = strlen (a);
int len_b = strlen (b);
int len = max (len_a, len_b);
int i, sum, j;
char a1[N], b1[N];
char score [N] = {'0'};
for (i = 0; i < N-1 ; i++)
{
score[i] = '0';
b1[i] = '0';
a1[i] = '0';
}
score[N-1] = '\0';
a1[N-1] = '\0';
b1[N-1] = '\0';//重置辅助数组
if (len_a > len_b)
{
for (j = 0 ; j < len_a - len_b ; j++)
{
b1[j] = '0';
}
for (; j < len_a ; j++)
{
b1[j] = b[j-len_a+len_b];
}
for (j = 0 ; j < len_a; j++)
{
b[j] = b1[j];
}
}
else if (len_a < len_b)
{
for (j = 0 ; j < len_b - len_a ; j++)
{
a1[j] = '0';
}
for (; j < len_b ; j++)
{
a1[j] = b[j-len_b+len_a];
}
for (j = 0 ; j < len_b; j++)
{
a[j] = a1[j];
}
}//对齐数字
for (i = len; i >= 1 ; i--)
{
sum = a[i-1] + b[i-1] + score[i] - '0'*3;
if (sum >= 10)
{
sum %= 10;
score[i-1] += 1;
}
score [i] = sum + '0';
}
if (score[0] != '0')
{
for (i = 0; i <= len ; i++)
{
printf("%c",score[i]);
}
putchar ('\n');
}
else
{
for (i = 1; i <= len ; i++)
{
printf("%c",score[i]);
}
putchar ('\n');
}
}
return 0;
}
int max (int a, int b)
{
return a > b ? a : b;
}
0.0分
3 人评分