hdoj 1877 又一版 A+B 水過~

                                  又一版 A+B

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


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
 

AC CODE:

#include<stdio.h>
int main()
{
    __int64 a,b,sum,array[100]={0};
    int m;
    while(scanf("%d",&m),m)
    {
		scanf("%I64d%I64d",&a,&b);
		sum=a+b;
		int i=0;
		if(a==0&&b==0)         //a,b可能都爲零
		{
			printf("0\n");
			continue;
		}
		while(sum)
		{
			array[i]=sum%m;
			sum/=m;
			i++;
		}
		while(i--)
			printf("%d",array[i]);
		printf("\n");
	}
    return 0;
}


C++ CODE:

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
	int m;
	__int64 A,B;
	char ans[30];
	while(cin>>m&&m)
	{
		cin>>A>>B;
		A+=B;
		itoa(A,ans,m);
		cout<<ans<<endl;
	}
	return 0;
}
 



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章