PAT 甲级 1023 Have Fun with Numbers(大数模拟)

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

Analysis:

BigInteger simulation problem.record times of every bit.if the original times equals the result's,print Yes.

May in java ,BigInteger class can also be accepted.

C++:

#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
	int num[20];
	string s;
	int vis[10]={0};
	cin>>s;
	int jin=0;
	for(int i=s.length()-1;i>=0;i--){
		vis[s[i]-'0']++;
		char t=s[i];//store the original value
		s[i]=((2*(s[i]-'0')+jin)%10)+'0';
		jin=(2*(t-'0')+jin)/10;
		vis[s[i]-'0']--;
	}
	for(int i=1;i<10;i++){
		if(vis[i]!=0){
			vis[0]=101;
			cout<<"No"<<endl;
			break;
		}
	}
	if(vis[0]!=101)cout<<"Yes"<<endl;
	if(jin!=0)cout<<jin;
	for(int i=0;i<s.length();i++){
		cout<<s[i];
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zpjlkjxy/article/details/83649981