# PAT Have Fun with Numbers 有几个测试点过不去

0

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<stdio.h>
#include<string.h>

void doublenum(char*, char*);   //双倍
int compare(char*, char*);      //比较两组数

int main()
{
char a[20], b[20] = {'\0'};
int i;
gets(a);
doublenum(a, b);
i = compare(a, b);
if( i == 10 ) printf("Yes\n");
else printf("No\n");

puts(b);
return 0;
}

void doublenum(char *a, char *b)
{
int i , z;
for( i=0; a[i]!='\0'; ++i){
z = (a[i]-'0')*2;
b[i] = z%10 + '0';
if( i != 0 )
b[i-1] += z/10;
}
}
int compare(char *a, char *b)
{
int i, z, c[10] = {0};          //创建一个数组里放0~9的个数
for( i=0; a[i]!='\0'; ++i){         //第一组加个数
for( z = 0; z<10; ++z)
if( a[i]-48 == z ) c[z]++;
}
for( i=0; a[i]!='\0'; ++i){         //第二组减个数
for( z = 0; z<10; ++z)
if( b[i]-48 == z ) c[z]--;
}
for( i=0; i<10; ++i )           //如果结果都为0，i为10， 否则，i<10
if( 0 != c[i] )
break;
return i;
}``````

り罐罐。 | 初学一级 | 园豆：46

0

KimBing_Ng | 菜鸟二级 |园豆：352 | 2020-02-22 19:58

り罐罐。 | 园豆：46 (初学一级) | 2020-02-23 00:07

您需要登录以后才能回答，未注册用户请先注册