题目地址:https://www.patest.cn/contests/pat-a-practise/1012
解题思路:用string 来保存大数计算的结果,然后将输入和输出分别映射成向量,进行比较。算法复杂度:O(length)
坑点:注意结果的逆序,注意判断is_p()函数变量的定义,不能加const修饰
代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
string double_n(const string &n)
{
string m;
string a;
int flag = 0;
for(int i = n.size() - 1; i >= 0; i--)
{
a += (2 * (n[i] - '0') + flag) % 10 + '0';
flag = (2 * (n[i] - '0') + flag) >= 10 ? 1 : 0;
}
if(flag == 1)
a += '1';
//cout << a << endl;
for(string::reverse_iterator it = a.rbegin(); it != a.rend(); it++)
m += *it;
return m;
}
bool is_p(string &n, string &m) //there is problem
{
string a = "0000000000", b = "0000000000";
for(string::iterator it = n.begin(); it != n.end(); it++)
a[*it-'0'] += 1;
for(string::iterator it = m.begin(); it != m.end(); it++)
b[*it-'0'] += 1;
if(a == b)
return true;
else
return false;
}
int main(void)
{
string line;
while(getline(cin, line))
{
string n, m;
stringstream ss(line);
ss >> n;
m = double_n(n);
if(is_p(n, m))
cout << "Yes" << endl;
else
cout << "No" << endl;
cout << m << endl;
}
return 0;
}