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 }

 

posted @ 2015-02-27 19:44  Joilee  阅读(149)  评论(0编辑  收藏  举报