PAT(大数)——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:
1234567899
Sample Output:
Yes
2469135798

题目大意:

一个大数乘以二。

题目解析:

字符串存储,每一位等于2*位+进位。

具体代码:

#include<iostream>
#include<algorithm>
using namespace std;
int ans[10];
int main()
{
    string pre,post;
    post.resize(22);
    cin>>pre;
    reverse(pre.begin(),pre.end());
    int flag=0;//进位
    for(int i=0;i<pre.size();i++){
    	ans[pre[i]-'0']++;
    	int tmp=2*(pre[i]-'0')+flag;
    	if(tmp>=10)
    		flag=1;
		else
			flag=0;
		ans[tmp%10]--;
		post[i]=tmp%10+'0';
	}
	if(flag==1){
		ans[1]--;
		post[pre.size()]='1';
	}
	for(int i=0;i<10;i++)
		if(ans[i]!=0){
			printf("No\n");
			for(int i=pre.size()+flag-1;i>=0;i--)
				printf("%c",post[i]);
			return 0;
		}
	printf("Yes\n");
	for(int i=pre.size()+flag-1;i>=0;i--)
		printf("%c",post[i]);
    return 0;
}
全部评论

相关推荐

10-25 12:05
已编辑
湖南科技大学 Java
若梦难了:我有你这简历,已经大厂乱杀了
点赞 评论 收藏
分享
在评审的大师兄很完美:像这种一般就是部门不匹配 转移至其他部门然后挂掉 我就是这样被挂了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务