Pat(Advanced Level)Practice--1023(Have Fun with Numbers)

来源:互联网 发布:mac chrome f12 编辑:程序博客网 时间:2024/06/06 09:00

Pat1023代码

题目描述:

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:
Yes2469135798

AC代码:
#include<cstdio>#include<cstring>#include<algorithm>#define MAX 25using namespace std;int main(int argc,char *argv[]){int i,j,len;char str[MAX];char str2[MAX];char ans[MAX];int temp;scanf("%s",str);len=strlen(str);str2[MAX-1]='\0';j=MAX-1;temp=0;for(i=len-1;i>=0;i--){temp=temp+(str[i]-'0')*2;str2[--j]=temp%10+'0';temp=temp/10;}if(temp!=0)//乘以2之后可能位数加一位str2[--j]=temp+'0';strcpy(ans,str2+j);sort(str,str+len);sort(str2+j,str2+MAX-1);if(strcmp(&str2[j],str)==0)printf("Yes\n");elseprintf("No\n");printf("%s\n",ans);return 0;}


0 0
原创粉丝点击