1023. Have Fun with Numbers (20)

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

来源: <http://www.patest.cn/contests/pat-a-practise/1023>

  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. }

来自为知笔记(Wiz)

时间: 2024-10-14 08:33:34

1023. Have Fun with Numbers (20)的相关文章

PAT Advanced 1023 Have Fun with Numbers (20) [?整数运算]

题目 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 i

1023 Have Fun with Numbers (20分)

模拟整数乘法,比较简单的乘法模拟,因为一个因数是2,只有一位.注意处理可能产生的进位,测试点2和7测的就是这个.(理解题意很重要,pat可能有的题不难,但是得仔细琢磨坑点在哪里) 用digit1[]记录原数字中各位数字的出现次数,digit2[]存储结果中各位数字出现的次数,逐个比较,如果次数不相等,可令标志flag=0,表明两数中有数字出现的数字不一样. 1 #include <iostream> 2 #include<cstdio> 3 #include<string&g

PAT (Advanced Level) 1023. Have Fun with Numbers (20)

手动模拟一下高精度加法. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; char s[30],t[30],c[30]; int lens,len

PAT:1023. Have Fun with Numbers (20) AC

#include<stdio.h> #include<string.h> char str[30]; //输入的数字 int tmp[30]; //*2后逆序的数字 int cntstr[10]; //输入数字0-9的个数 int tmpI=0,jin=0; //*2的时候的int数组下标和进位数 void Double(char* str,int len1) { memset(tmp,0,sizeof(tmp)); //将str*2并按个位在左,高位在右方式存储 for(int

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

00-自测4. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 24691

PAT 1069. The Black Hole of Numbers (20)

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in

pat1069. The Black Hole of Numbers (20)

1069. The Black Hole of Numbers (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-

1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise

题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new n

1100. Mars Numbers (20)【字符串处理】——PAT (Advanced Level) Practise

题目信息 1100. Mars Numbers (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep,