问题描述:给定一个数值A,两倍后B,判断AB出现的数字个数是否相等。
解题思路:字符串数值加法,标记数组
AC代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
freopen("test.txt", "r", stdin);
string str, s = "";
cin >> str;
int n = str.length(), a = 0;
vector<int>tag(10, 0), ntag(10, 0);
for (int i = n-1; i >= 0; --i) {
int x = str[i] - '0';
++tag[x];
x = x * 2 + a;//翻倍+进位
a = x / 10;//进位
x %= 10;//结果
++ntag[x];
s += x + '0';
}
if (a) {
s += a + '0';
++ntag[1];
}
reverse(s.begin(), s.end());
if (tag != ntag) printf("No\n");//数组可以比较
else printf("Yes\n");
printf("%s", s.c_str());
return 0;
}