1022 D进制的A+B

给定的范围是2^30-1,如果是转化成2进制,也得30位,所以
不管用int或者long long都是没办法接收的(int大概是9位,long long是19位)只能用数组/栈了。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> tentoD(ll a,int d){//1到10进制 
	vector<int> res;
	while(a){
		res.push_back(a % d);
		a /= d;
	}
	return res;
}
int main(){
	ll a,b,d;
	cin>>a>>b>>d;//10进制转化为d进制 
	ll res = a + b;
    vector<int> ans = tentoD(res,d);
    if(ans.size()==0){
    	cout << "0";
    	return 0;
	}
    for(int i=ans.size()-1;i>=0;i--){
    	cout << ans[i];
	}
	return 0;
}
posted @ 2024-04-15 20:10  YuKiCheng  阅读(24)  评论(0编辑  收藏  举报