PAT甲级-1023 Have Fun with Numbers

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

题目意思大概是给一个数,判断它里面包含的每一位数字的个数和它乘2之后包含的每一位数字的个数是否相等。
解题思路:首先注意这个数最大是20位数,这时候longlong类型也不够,所以肯定是要考虑字符串或字符输入,考虑到如果用字符串的话需要从最后一位算,但是乘2之后加到字符串上不方便,所以用字符数组的输入方式,然后可能最后会产生进位所以可能结果会是21位,然后可以用一个数组记录每个数出现的次数,输入数的时候遇到一个数字就对应的加一,乘二之后对应减一,最后遍历数组如果都为0那么就是Yes,否则是No。(也可以用map)
还有一种方法,因为输入的都是数字所以可以排序,排序之后比较字符串是否相等即可得出结论。

#include<iostream>
using namespace std;

/*-----------------------------------------哈希表处理-----------------------------------------*/
int main()
{
	int num[10]={0};//存放每一位数字的个数
	int a[20],b[21];//a存储输入的数,b存储乘2之后的数
	string s;
	cin>>s;
	int i,jinwei=0;
	for(i=0;i<s.size();i++) {
		a[i]=s[s.size()-i-1]-'0';
		num[a[i]]++;
	}
	for(i=0;i<s.size();i++) {
		b[i]=(a[i]*2+jinwei)%10;
		if(a[i]*2+jinwei>=10) jinwei=1;
		else jinwei=0;
        num[b[i]]--;
	}
	if(jinwei==1) {
		b[i]=1;
		num[1]--;
	}
	for(i=0;i<10;i++) if(num[i]!=0) break;
	if(i==10) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
    if(b[s.size()]==1) cout<<"1";
	for(i=s.size()-1;i>=0;i--) cout<<b[i];
	return 0;
}
/*-----------------------------------------排序后比较字符串-----------------------------------------*/
/*
string sort_s(string n)
{
    int temp[20];string s;
    for(int i=0;i<n.size();i++) temp[i]=n[i]-'0';
    sort(temp,temp+n.length());//把传入的字符串分到数组里进行排序
    for(int i=0;i<n.length();i++) s+=temp[i];
    return s;
}

int main()
{
    string n;string nn;int m[20],x[21];int jinwei=0;
    cin>>n;
    for(int i=n.size()-1;i>=0;i--) m[n.size()-i-1]=n[i]-'0';//m存放每一位数字
    for(int i=0;i<n.size();i++) {
        x[i]=(m[i]*2+jinwei)%10;             //下面一种方法可以优化
        if((m[i]*2+jinwei)>=10) jinwei=1;
        else jinwei=0;
    }
    if(jinwei==1) {
        x[n.size()]=1;
        cout<<"No"<<endl;//多出来一位肯定不会相等
        for(int i=n.size();i>=0;i--) nn+=x[i]+'0';
    }else{
        for(int i=n.size()-1;i>=0;i--) nn+=x[i]+'0';
        if(sort_s(n)==sort_s(nn)) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    cout<<nn;
    return 0;
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值