• 1023 Have Fun with Numbers (20 分)


    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 kdigits, 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

    直接用字符串读入更好处理。
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int vis[10];
     4 string s;
     5 bool flag = true;
     6 
     7 int main(){
     8     cin >> s;
     9     for(int i = 0; i < s.length(); i++){
    10         int x = s[i] - '0';
    11         vis[x]++;
    12     }
    13     int ans = 0;
    14     for(int i = s.length()-1; i >= 0; i--){
    15         int x = s[i] - '0';
    16         int k = x*2 + ans;
    17         int an = k%10;
    18         s[i] = an + '0';
    19         if(vis[an] >= 1)
    20             vis[an]--;
    21         else{
    22             flag = false;
    23         }
    24         ans = k/10;
    25     }
    26     if(ans == 1)
    27         s = '1'+ s;
    28     if(flag)
    29         cout <<"Yes"<<endl;
    30     else
    31         cout <<"No"<<endl;
    32     cout <<s<<endl;
    33     return 0;
    34 }
  • 相关阅读:
    Git学习笔记(一)
    sql复制表结构及复制表数据
    C#连接Oracle数据库查询数据
    ExcelHelper.cs
    MongoHelper.cs
    Accesshelper.cs
    Android_布局属性大全
    [WinForm]Dundas Chart控件学习(附源码)
    [Android]Adb connection Error:远程主机强迫关闭了一个现有的连接
    html to openxml
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11048905.html
Copyright © 2020-2023  润新知