HDOJ 1877

又一版 A+B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9541    Accepted Submission(s): 3551


Problem Description
输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。



 

 

Input
输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。
 

 

Output
输出格式:每个测试用例的输出占一行,输出A+B的m进制数。
 

 

Sample Input
8 1300 48 2 1 7 0
 

 

Sample Output
2504 1000
 
注意:虽然说的是非负,但是(<=231-1),用int即可,用unsigned时会AV
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void convert_output(int num,int m)
{
    int i,j,k;int temp;
    int ch[30];
    //memset(ch,0,sizeof(ch));
    for(i=0;i<30;i++)
        ch[i]=0;
    j=0;
    while(num>0)
    {
        ch[j++]=num%m;
        num/=m;
    }
   /*
   写成了i++,一直内存错误,
   以后再出现内存错误 
   往这方面想
   */ 

//不必分j是否为1, for(i=j-1;i>=1;i--) printf("%d",ch[i]); printf("%d\n",ch[0]); } int main() { int i,j;int m; int a,b,ans; while(scanf("%d%d%d",&m,&a,&b),m) { ans=a+b; convert_output(ans,m); ans=0; } return 0; }

 

posted @ 2012-07-25 19:02  加拿大小哥哥  阅读(221)  评论(0)    收藏  举报