题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805478658260992
代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//比较两个数是否具有相同的数字
int find(vector<int> list1, vector<int> list2){
int flag = 0;
for(int i=0; i<list1.size(); i++){
flag = 0;
for(int j=0; j<list2.size()-1; j++){
if(list1[i] == list2[j]){
flag = 1;
break;
}
}
if(flag == 0)
return 0;
}
return 1;
}
int main(){
vector<int> list1;
string num;
cin >> num;
for(int i=num.size()-1; i>=0; i--){
list1.push_back(num[i] - '0');
}
vector<int> list2(list1.size()+1, 0);
//不考虑进位乘2计算
for(int i=0; i<list1.size(); i++){
list2[i] += 2 * list1[i];
}
//处理进位
for(int i=0; i<list2.size()-1; i++){
list2[i+1] += list2[i] / 10;
list2[i] %= 10;
}
if(list2[list2.size()-1] != 0){//位数不同,直接输出no
cout << "No" << endl;
}
else{
if(find(list1, list2))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
if(list2[list2.size()-1] != 0)
cout << list2[list2.size()-1];
for(int i=list2.size()-2; i>=0; i--){
cout << list2[i];
}
return 0;
}