文章
9
粉丝
126
获赞
11
访问
52.0k
新手,轻喷
#include <stdio.h>
#include <string.h>
char a[1005];
char b[1005];
char c[1005];
void reverse (char * a, int len)//逆置
{
for (int i = 0; i < len/2; i++)
{
char temp = a[i];
a[i] = a[len - i - 1];
a[len - i - 1] = temp;
}
}
int addzero(char * a, char * b, int len_a, int len_b)//较小数字后面添0
{
int len;
if (len_a > len_b)
{
len = len_a;
for (int i = len_b; i < len; i ++)
b[i] = '0';
}
else
{
len = len_b;
for (int i = len_a; i < len; i ++)
a[i] = '0';
}
return len;
}
int main ()
{
while (scanf ("%s%s", a, b) != EOF)
{
int len_a = strlen(a);
int len_b = strlen(b);
int len;
reverse (a, len_a);
reverse (b, len_b);//逆置
len = addzero (a, b, len_a, len_b);
int carry = 0;
for (int i = 0; i < len; i ++)//相加
{
c[i] = (a[i] - '0' + b[i] - '0' + carry) % 10 + '0';
carry = (a[i] - '0' + b[i] - '0' + carry) / 10 ;
}
int len_c;
...
登录后发布评论
暂无评论,来抢沙发