又一版 A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10195 Accepted Submission(s): 3775
当m为0时输入结束。
8 1300 48 2 1 7 0
2504 1000
//该题主要考虑是0的情况。
#include <stdio.h>
#include <string.h>
void detom(int a, int *ma, int m)
{
int i = 0;
while (a)
{
ma[i++] = a%m;
a /= m;
}
}
void plusTwo(int *ma, int *mb, int m)
{
int i = 0;
int d = 0;
int t;
for(i = 0; i < 40; i++)
{
t = ma[i] + mb[i] + d;
ma[i] = t % m;
if(t < m)
d = 0;
else
d = 1;
}
}
int main ()
{
int a, b, m, i;
int ma[40], mb[40];
while (~scanf ("%d", &m), m)
{
memset(ma, 0, sizeof(ma));
memset(mb, 0, sizeof(mb));
scanf ("%d%d", &a, &b);
detom(a, ma, m);
detom(b, mb, m);
plusTwo(ma, mb, m);
i = 39;
while (ma[i] == 0) i--;
if(i < 0)//是0的情况
printf("0");
else
for(; i >=0; i--)
printf("%d", ma[i]);
printf("\n");
}
return 0;
}
173




被折叠的 条评论
为什么被折叠?



