很简单,但是题中给的是不到20位的数字,已经超出了Int、long、long long的范围,double有效长度37足够了,但是不能用%运算,没查为什么,转为string接受数字。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string org;
cin >> org;
if (org == "0")
{
cout << "Yes" << endl;
cout << 0;
return 0;
}
string db;
int ll = org.size();
int a[10] = { 0 }, b[10] = { 0 }, ans[20] = { 0 }, i;
int temp = 0, head = 0, j = 0;
for (i = ll - 1; i >= 0; i--)
{
//cout <<"i=="<< i << endl;
temp = (org[i] - '0') * 2;
//cout << "temp="<<temp << endl;
a[(org[i] - '0')]++;
if (temp +head>=10)
{
ans[i]= temp - 10 + head;
head = 1;
}
else
{
ans[i] =temp+head;
head = 0;
}
b[ans[i]]++;
//j++;
}
int flag = true;
if (head)
{
cout << "No" << endl;
cout << head;
flag = false;
}
else
{
for (i = 0; i < 10; i++)
{
if (a[i] != b[i])
{
cout << "No" << endl;
flag = false;
break;
}
}
}
if (flag)
{
cout << "Yes" << endl;
}
for (i = 0; i < ll; i++)
{
cout << ans[i];
}
system("pause");
return 0;
}
以后不能眼高手低了,这种题应该半小时想加写,给15min进行完善。很久不练手生了,来日再战!