PAT00-自测4. Have Fun with Numbers (20)

时间:2021-01-23 17:03:14


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 file 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>
#define LEN 20
int main()
{
 int i=0;
 int j=0;
 int array[10]={0};
 usigned long long num1,num2,num3,num4;
 scanf("%llu",&num1);
 num4=num1;
 num2=num1*2;
 num3=num2;
 while(num1){
  array[num1%10]++;
  num1=num1/10;
 }
 while(num2)
 {
  array[num2%10]--;
  num2=num2/10;
 } 
 
 int flag=1;
 for(i=0;i<=9;i++)
 {
   
  if(array[i]!=0){
   flag=0;
   break;
  }
 }
 if(flag==1)
  printf("Yes\n");
 else
  printf("No\n");
 printf("%llu",num3);
 //printf("%llu",num4);  
 
} 
这段代码的结果是部分正确,因为usigned long long 最大值为2^64=18446744073709551616
预算20位数时会溢出,找不到更大的数据类型来存储所以不能偷懒了,换数组存储。