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;
}
全部评论

相关推荐

码农索隆:单休一个月少休息4天,一年就是48天,平时节假日,别人3天假期,单休的两天
点赞 评论 收藏
分享
完美的潜伏者许愿简历...:隐藏信息被你提取出来了,暗示,这就是暗示
点赞 评论 收藏
分享
点赞 评论 收藏
分享
门口唉提是地铁杀:之前b站被一个游戏demo深深的吸引了。看up主页发现是个初创公司,而且还在招人,也是一天60。二面的时候要我做一个登录验证和传输文件两个微服务,做完要我推到github仓库,还要我加上jaeger和一堆运维工具做性能测试并且面试的时候投屏演示。我傻乎乎的做完以后人家跟我说一句现在暂时不招人,1分钱没拿到全是白干
你的秋招第一场笔试是哪家
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 13:47
机械打工仔:你自己匿名可以,这么好的公司就别给它匿名了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务