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.

输入

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

输出

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.

样例输入 

1234567899

样例输出 

Yes
2469135798

题意理解

给你不超过20位的整数,那么有点大,我们直接用字符串来存

然后让你判断,这个数的两倍是否还是原来数的组合

是的话输出Yes 否则输出No

无论是不是都要输出这个数的两倍

什么是原来数字的组合呢?

就是这个数字用的位数和原来相同 比如样例 1234567899

数位总共有1个1 1个2 1个3 1个4 1个5 1个6 1个7 1个8 2个9

而它的两倍2469135798 数位上面也是一样的数字个数 当然位置可以变

那么就输出Yes

这题主要是高精度的加法

t是进位

t%10就是在这一位A+A的数字我们应该存放的数位是多少

t/=10就是我们存好这一位以后我们应该留下的进位

最后翻转一下就是我们想要的两倍数

代码 

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int cnta[10],cntb[10];
string add(string a){
    vector<int>A;
    for (int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
    vector<int> C;
    int t=0;
    for (int i=0;i<A.size();i++){
        t+=A[i]+A[i];
        C.push_back(t%10);
        t/=10;
    }
    if(t)C.push_back(t);
    string c="";
    reverse(C.begin(),C.end());
    for(int i=0;i<C.size();i++){
        c+=C[i]+'0';
    }
    return c;
}
int main(){
    string a;
    cin>>a;
    for(int i=0;i<a.size();i++)
       cnta[a[i]-'0']++;
    
    string b=add(a);
    
    for(int i=0;i<b.size();i++)
        cntb[b[i]-'0']++;
    
    int fl=1;
    for(int i=0;i<=9;i++){
        if(cnta[i]!=cntb[i]){
            fl=0;
            break;
        }
    }
    if(fl)puts("Yes");
    else puts("No");
    cout<<b<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值