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
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
int b[21], a[21],q[21];
char c[21],d[21];
while (scanf("%s", c) != EOF){
memset(b, 0, sizeof(b));
memset(a, 0, sizeof(a));
memset(q, 0, sizeof(q));
int flag = 0;
int n = 0;
int i = 0;
int l = strlen(c);
for (l; l--; l >= 0){
n = c[l] - '0';
a[i] = n;
b[i] += (2*n)%10;
q[i] += (2*n)%10;
if ((2*n)/10){
b[i + 1]++;
q[i + 1]++;
}
i++;
}
if (a[i]==0&&b[i]!=0){
printf("No\n");
for (int j = i; j>=0; j--){
printf("%d", q[j]);
}
printf("\n");
continue;
}
sort(a, a + i+1);
sort(b, b + i+1);
for (int j = 0; j <= i; j++){
if (a[j] != b[j]){
printf("No\n");
for (int k = i-1; k>=0; k--){
printf("%d", q[k]);
}
printf("\n");
flag = 1;
break;
}
}
if (flag == 0){
printf("Yes\n");
for (int j = i-1 ;j>=0; j--){
printf("%d", q[j]);
}
printf("\n");
}
}
system("pause");
return 0;
}
啊啊啊啊废了n久的博客又又又重新营业了 弱鸡要准备复试所以要刷题,刷的PTA 乙级QAQ 希望好运降临啊啊啊~
正文:
第一个注意点是 数字不超过20.也就是21就不可了,,但是unsigned long long int 只能到19,所以输入要用字符串
第二个是 双倍之后最高位不能有进位,否则是NO。
第三就是数字要一一对应,,所以我选择了sort排序之后依次比较。。。。
想不到最优解。。。。只能水题了。