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 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:1234567899Sample Output:
Yes 2469135798
点击打开链接Pat甲 1023
题意就是看这个数字乘以二后,和原来数字的组成是不是相同,用两个数组标记下比一下就行了
坑点是如果组成不同。也需要打印数字的两倍。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int num[30],res[30],count1[10],count2[10];
char str1[30];
int main()
{
scanf("%s",&str1);
int len=strlen(str1);
int index=29;
for(int i=len-1;i>=0;i--)
{
num[index--]=str1[i]-'0';
count1[str1[i]-'0']++;
}
int carry=0;
for(int i=29;i>=0;i--)
{
res[i]=(carry+num[i]*2)%10;
carry=(carry+num[i]*2)/10;
}
index=0;
while(res[index]==0)
index++;
for(int i=index;i<=29;i++)
count2[res[i]]++;
for(int i=0;i<=9;i++)
{
if(count1[i]!=count2[i])
{
printf("No\n");
for(int k=index;k<=29;k++)
{
printf("%d",res[k]);
}
printf("\n");
return 0;
}
}
printf("Yes\n");
for(int i=index;i<=29;i++)
{
printf("%d",res[i]);
}
printf("\n");
return 0;
}