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


Pat 1023コード
タイトルの説明:
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

ACコード:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 25

using 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
"); else printf("No
"); printf("%s
",ans); return 0; }