码迷,mamicode.com
首页 > 其他好文 > 详细

1023. Have Fun with Numbers (20)

时间:2015-12-06 11:27:41      阅读:140      评论:0      收藏: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:
1234567899
Sample Output:
Yes
2469135798

 

  1. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. int main(void) {
  7. string s1 = "12345678901234567890",s2="12345678901234567890";
  8. cin >> s1;
  9. int n = s1.length();
  10. int jinwei = 0;
  11. for (int i = 0; i < n; i++) {
  12. if(s1[i]<‘5‘)
  13. s2[i] = 2 * (s1[i] - ‘0‘)+‘0‘;
  14. else {
  15. if (i > 0) {
  16. s2[i] = 2 * (s1[i] - ‘0‘) - 10+‘0‘;
  17. s2[i - 1] += 1;
  18. }
  19. if (i == 0) {
  20. s2[i] = 2 * (s1[i] - ‘0‘) - 10+‘0‘;
  21. jinwei = 1;
  22. }
  23. }
  24. }
  25. if (jinwei == 1) {
  26. cout << "No" << endl;
  27. cout << 1;
  28. for (int i = 0; i < n; i++) {
  29. cout << s2[i];
  30. }
  31. return 0;
  32. }
  33. int a[20] = { 0 };
  34. for (int i = 0; i < n; i++) {
  35. for (int j = 0; j < n; j++) {
  36. if (s2[i] == s1[j] && a[j] == 0)
  37. a[j]++;
  38. }
  39. }
  40. int count = 0;
  41. for (int i = 0; i < n; i++)
  42. if (a[i] == 1)
  43. count++;
  44. if (count == n) {
  45. cout << "Yes"<<endl;
  46. for (int i = 0; i < n; i++) {
  47. cout << s2[i];
  48. }
  49. }
  50. else {
  51. cout << "No"<<endl;
  52. for (int i = 0; i < n; i++) {
  53. cout << s2[i];
  54. }
  55. }
  56. return 0;
  57. }





1023. Have Fun with Numbers (20)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!