A1023. 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
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <iostream>
  4 #include <string.h>
  5 #include <string>
  6 #include <math.h>
  7 #include <algorithm>
  8 using namespace std;
  9
 10 struct bign
 11 {
 12     int d[1010];
 13     int len;
 14     bign(){
 15         memset(d,0,sizeof(d));
 16         len=0;
 17     }
 18 };
 19
 20 bign change(char str[]){
 21     bign a;
 22     a.len=strlen(str);
 23     for(int i=0;i<a.len;i++)
 24     {
 25         a.d[i]=str[a.len-i-1]-‘0‘;
 26     }
 27     return a;
 28 }
 29
 30 bign divide(bign a,int b,int & r)
 31 {
 32     bign c;
 33     c.len=a.len;
 34     for(int i=a.len-1;i>=0;i--)
 35     {
 36          r=10*r+a.d[i];
 37          if(r<b)c.d[i]=0;
 38          else
 39          {
 40           c.d[i]=r/b;
 41           r=r%b;
 42          }
 43     }
 44     while(c.len-1>0&&c.d[c.len-1]==0)
 45     {
 46         c.len--;
 47     }
 48     return c;
 49 }
 50
 51
 52 bign multi(bign a,int b)
 53 {
 54     bign c;
 55     int carry=0;
 56     for(int i=0;i<a.len;i++)
 57     {
 58         int temp;
 59         temp=a.d[i]*b+carry;
 60         carry=temp/10;
 61         c.d[c.len++]=temp%10;
 62     }
 63     //乘法高位处理
 64     while(carry!=0)
 65     {
 66         c.d[c.len++]=carry%10;
 67         carry/=10;
 68     }
 69     return c;
 70 }
 71
 72 void print(bign a)
 73 {
 74   for(int i=a.len-1;i>=0;i--)
 75   {
 76       printf("%d",a.d[i]);
 77   }
 78 }
 79
 80 bool judge(bign a ,bign b)
 81 {
 82     if(a.len!=b.len)return false;
 83     int count[10]={0};
 84     for(int i=0;i<a.len;i++ )
 85     {
 86         count[a.d[i]]++;
 87         count[b.d[i]]--;
 88     }
 89     for(int i=0;i<10;i++)
 90     {
 91         if(count[i]!=0)return false;
 92     }
 93     return true;
 94 }
 95 int main(){
 96     char str[21];
 97     scanf("%s",str);
 98     bign a=change(str);
 99     bign b=multi(a,2);
100     if(judge(a,b)==false)
101     {
102         printf("No\n");
103     } else
104     {
105         printf("Yes\n");
106     }
107     print(b);
108     return 0;
109 }
时间: 2024-10-04 22:43:23

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

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,

1120 Friend Numbers (20 分)

1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same sum of their digits, and the sum is their "friend ID". For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend I

PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)

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

1100. Mars Numbers (20)

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, oct, nov, dec" on Mars, respectively. For the next higher dig

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