您的位置:首页 > 其它

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

2014-11-16 16:00 459 查看


1023. Have Fun with Numbers (20)

时间限制

400 ms

内存限制

32000 kB

代码长度限制

16000 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 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


提交代

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
int i,la,lb,flag=1,jw;
int a[30],b[30];
char ch[30];
int count_a[10]={0},count_b[10]={0};
scanf("%s",ch);
la=strlen(ch);
lb=la;
for(i=0;i<la;i++)
{
a[la-1-i]=ch[i]-'0';
}
jw=0;
for(i=0;i<la;i++)
{
b[i]=a[i]*2+jw;
jw=0;
if(b[i]>=10)
{
jw=1;
b[i]-=10;
}
}
if(jw==1)
{
b[lb]=jw;
lb++;
}
for(i=0;i<la;i++)
count_a[a[i]]++;
for(i=0;i<lb;i++)
count_b[b[i]]++;
for(i=0;i<10;i++)
if(count_a[i]!=count_b[i])
flag=0;
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
for(i=lb-1;i>=0;i--)
cout<<b[i];
cout<<endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: