• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
村雨sup
自己选的路,跪着也要走完 XD
博客园    首页    新随笔    联系   管理    订阅  订阅
PAT 1023 Have Fun with Numbers
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 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxnum 100005

int a[30] = {0};
int count1[10] = {0}; //分别对倍乘前后的位计数
int count2[10] = {0};

int main(){
    string s;
    cin >> s;
    int len = s.size();
    for(int i=0;i < s.size();i++){
        a[i] = s[i]-'0';
    }
    for(int i=0;i < len;i++){
        count1[a[i]]++;
    }
    for(int i=0;i < len/2;i++){ //翻转
        swap(a[i],a[len-i-1]);
    }

    int jinwei = 0;   //倍乘
    for(int i=0;i <= len;i++){
        int num = a[i]*2 + jinwei;
        a[i] = num%10;
        jinwei = (num)/10;
    }

//    for(auto num:a) cout << num << " ";

    int pos = 0;
    for(int i=30;i >= 0;i--){
        if(a[i]){pos = i;break;}
    }
    for(int i=0;i <= pos;i++){
        count2[a[i]]++;
    }

    int flag = 1;
    for(int i=0;i < 10;i++){
        if(count1[i] != count2[i])flag = 0;
    }
    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;

    for(int i=pos;i >= 0;i--){
        cout << a[i];
    }


    return 0;
}

_

 
posted on 2019-04-12 16:08  村雨sup  阅读(166)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3