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
用数组表示,进行加法
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include <math.h> #include <stdio.h> using namespace std; int a[25]; int c[25]; char b[25]; int tag[10]; int len; int add() { int i=0; int num=0; while(i<len||num!=0) { a[i]+=(a[i]+num); num=a[i]/10; a[i]%=10; i++; } return i; } int main() { scanf("%s",b); len=strlen(b); memset(tag,0,sizeof(tag)); memset(a,0,sizeof(a)); for(int i=0;i<len;i++) { a[len-1-i]=b[i]-'0'; tag[b[i]-'0']++; } int cnt=add(); for(int i=0;i<cnt;i++) { tag[a[i]]--; } bool ans=true; for(int i=0;i<=9;i++) { if(tag[i]!=0) ans=false; } if(!ans) printf("No "); else printf("Yes "); for(int i=cnt-1;i>=0;i--) printf("%d",a[i]); printf(" "); return 0; }
提交代码