我是靠谱客的博主 踏实蛋挞,最近开发中收集的这篇文章主要介绍PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测4. Have Fun with Numbers (20) 【二星级】00-自测4. Have Fun with Numbers (20),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题目链接:http://www.patest.cn/contests/mooc-ds/00-%E8%87%AA%E6%B5%8B4


题面:

00-自测4. 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之后,得到的新数字包含的每个数字的个数是否与原数字包含的数量相同。题意是比较绕的,说什么乘二又乘二,又什么1-9。其实,就是只乘了1遍2,数字也可以包含0。数据是比较水的,这也是PAT比不上ACM的地方,网上一组只数1-9的也过了。


解题:

    因为数字有20位,long long也不够用,所以就自己手动模拟一下乘2的过程吧。


代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
//计数,存储
int cnt1[10],cnt2[10],num1[25],num2[25],p=0,f=0,x;
string s;
bool flag=true;
memset(cnt1,0,sizeof(cnt1));
memset(cnt2,0,sizeof(cnt2));
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
//输入
cin>>s;
for(int i=s.length()-1;i>=0;i--)
{
num1[p++]=s[i]-'0';
}
//模拟乘2
for(int i=0;i<25;i++)
{
x=num1[i]*2+f;
num2[i]=x%10;
f=x/10;
}
//计数
for(int i=0;i<s.length();i++)
{
cnt1[num1[i]]++;
cnt2[num2[i]]++;
}
//特判最高位
if(num2[s.length()])cnt2[s.length()]++;
for(int i=0;i<10;i++)
if(cnt1[i]!=cnt2[i])
{
flag=false;
break;
}
//输出
if(flag)cout<<"Yesn";
else cout<<"Non";
flag=false;
//如果是0,那么p没有被赋值,输出0
p=0;
//去前导0
for(int i=24;i>=0;i--)
{
if(num2[i])
{
p=i;
break;
}
}
//输出
for(int i=p;i>=0;i--)
cout<<num2[i];
cout<<endl;
return 0;
}


最后

以上就是踏实蛋挞为你收集整理的PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测4. Have Fun with Numbers (20) 【二星级】00-自测4. Have Fun with Numbers (20)的全部内容,希望文章能够帮你解决PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测4. Have Fun with Numbers (20) 【二星级】00-自测4. Have Fun with Numbers (20)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部