1023 Have Fun with Numbers (20分)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.
Sample Input:
1234567899
Sample Output:
Yes 2469135798
#include<iostream>
#include "stack"
using namespace std;
char number[20];
int map[11]={0};
stack<int> digit;
stack<int> re;
char fin[20];
int main(){
scanf("%s",number); //以字符串形式输入数字
int flag=0;
for (int i=0; number[i]!='\0'; i++) {
digit.push(number[i]-'0'); //把数字从左到右压入栈
map[number[i]-'0']++; //对应的数字出现次数+1
}
while (!digit.empty()) { //
int result=(digit.top()*2+flag)%10;
re.push(result);
flag=digit.top()*2/10;
digit.pop();
}
if (flag!=0) {
re.push(flag); //最后的进位压入栈
}
int i=0;
int flag2=1;
while(!re.empty()) {
map[re.top()]--;
if (map[re.top()]<0) {
flag2=0;
}
fin[i++]=re.top();
re.pop();
}
if (flag2==1) {
printf("Yes\n");
}
else{
printf("No\n");
}
for(int j=0;j<i;j++) {
printf("%d",fin[j]);
}
}