Codeup_1941_又一版A+B

題目描述

輸入兩個不超過整型定義的非負10進制整數A和B(<=231-1),輸出A+B的m (1 < m <10)進制數。

輸入

輸入格式:測試輸入包含若干測試用例。每個測試用例佔一行,給出m和A,B的值。
當m爲0時輸入結束。

輸出

輸出格式:每個測試用例的輸出佔一行,輸出A+B的m進制數。

樣例輸入

2 4 5
8 123 456
0

樣例輸出

1001
1103

提示

注意輸入的兩個數相加後的結果可能會超過int和long的範圍。

代碼:

#include <cstdio>
#include <stack>
using namespace std;

typedef long long LL;

int main()
{
	int a,b,m;
	while(scanf("%d",&m)&&m != 0){
		scanf("%d %d",&a,&b);
		LL sum = (LL)a+b;
		if(sum == 0){	//error1:忽略了當 sum = 0 時 while 內語句不會執行。 
			printf("0\n");
			continue;
		}
		stack<int> si;
		while(sum != 0){
			si.push(sum%m);
			sum = (LL)sum/m;
		}
		while(!si.empty()){
			printf("%d",si.top());
			si.pop();
		}
		printf("\n");
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章