1023. Have Fun with Numbers (20)

清疚 2022-05-31 10:05 275阅读 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 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:

  1. 1234567899

Sample Output:

  1. Yes
  2. 2469135798

题目大意:

注意到数字123456789是一个9位数,有数字1到数字9组成,没有重复。我们将得到246913578,这恰好是另一个9为数字,只是排列顺序不同。如果我们再把他翻倍,看看结果如何!
现在您的任务是检查是否有更多的数字由此属性。也就是说给定一个K位数字加倍,你的任务是判断结果数字是否由原来的数字组成。
输入规格:
每个输入文件包含一个测试用例。每个测试用例包含一个不超过20位的正整数。
输出规范:
对于每个测试用例,如果加倍数字由原来的数字组成,那么打印“Yes”否则打印“No”然后再下一行打印出加倍的数字。

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5. int i,j,n,m,k,t,flag=0;
  6. char str[21],result[30];
  7. int arr[10],brr[10];
  8. memset(arr,0,sizeof(arr));
  9. memset(brr,0,sizeof(arr));
  10. scanf("%s",str);
  11. for(i=0;str[i]!='\0';i++)
  12. {
  13. arr[str[i]-'0']++;
  14. }
  15. k=0;
  16. for(i=strlen(str)-1,j=0;i>=0;i--)
  17. {
  18. n=str[i]-'0';
  19. m=n*2;
  20. m+=k;
  21. k=m/10;
  22. result[j++]=m%10+'0';
  23. }
  24. if(k!=0)
  25. {
  26. result[j++]=k+'0';
  27. }
  28. result[j]='\0';
  29. for(i=0;result[i]!='\0';i++)
  30. {
  31. brr[result[i]-'0']++;
  32. }
  33. for(i=0;i<10;i++)
  34. {
  35. if(arr[i]!=brr[i])
  36. break;
  37. }
  38. if(i==10)
  39. {
  40. printf("Yes\n");
  41. }
  42. else
  43. {
  44. printf("No\n");
  45. }
  46. for(i=strlen(result)-1;i>=0;i--)
  47. printf("%c",result[i]);
  48. return 0;
  49. }

发表评论

表情:
评论列表 (有 0 条评论,275人围观)

还没有评论,来说两句吧...

相关阅读

    相关 1023. 组个最小数 (20)

    给定数字0-9各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意0不能做首位)。例如:给定两个0,两个1,三个5,一个8,我们得到的最