杭电acm1877

http://acm.hdu.edu.cn/showproblem.php?pid=1877

感觉应该是很水的一题,但是代码却是wa了,在网上找到一个牛逼的函数itoa(a+b,ch,m);能够自动按m进制转换

错误代码

View Code
 1 #include
 2 void print(long n,int m)
 3 {
 4    char s[100000];
 5    int i=0;
 6    while(n)
 7    {
 8        s[i++]=n%m+'0';
 9        n/=m;
10    }
11    i--;
12    while(i>=0)
13    printf("%c",s[i--]);
14    printf("\n");
15 }
16 int main()
17 {
18    long  a,b;
19    int m;
20    while(scanf("%d",&m)&&m)
21    {
22       scanf("%ld%ld",&a,&b);
23       b+=a;
24       print(b,m);
25    }
26    return 0;
27 }

ac代码

View Code
 1 #include
 2 #include
 3 int main(){
 4     int m;
 5     long a,b;
 6     char ch[10005];
 7     while(scanf("%d",&m) && m){
 8         scanf("%ld%ld",&a,&b);
 9         itoa(a+b,ch,m);   //把一整数转换为字符串,即将sum转换为m进制数,,返回到ch字符串
10         printf("%s\n",ch);
11     }
12     return 0;
13 }

 

转载于:https://www.cnblogs.com/huzhenbo113/articles/3012606.html

你可能感兴趣的:(杭电acm1877)