【1023 20 大整数加法】 Have Fun with Numbers

传送门

题意

给定一个数字 \(n\) ,求 \(n\times 2\) 后的数字是否是数字 \(n\) 各位上数字的重新排列

数据范围

\(1\leq |n|\leq 20\)

题解

  • 大整数模拟运算,用字符串表示
  • 最后运算最高位存在进位的数据

Code

#include <bits/stdc++.h>
using namespace std;

map<char, int> mp;

int main() {
	string num; cin >> num;
	int in = 0;
	
	string res = "";
	for (int i = num.size() - 1; i >= 0; i--) {
		mp[num[i]]++;
		int x = (num[i] - '0') * 2 + in;
		res += (x % 10 + '0');
		in =  x / 10;
	}
	if (in) res += '1';
	bool flag = 1;
	for (int i = 0; i < res.size(); i++) {
		if (mp[res[i]] == 0) {
			flag = 0;
			break;
		}
		mp[res[i]]--;
	}
	reverse(res.begin(), res.end());
	if (flag) cout << "Yes" << endl;
	else cout << "No" << endl;
	cout << res << endl;
}
posted @ 2021-02-13 15:04  Hyx'  阅读(60)  评论(0)    收藏  举报