zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    WM11破解版
    安装系统
    linux配置问题
    PAT:1022. Digital Library (30) (部分正确,错最后两个)
    PAT:1023. Have Fun with Numbers (20) AC
    PAT:1081. Rational Sum (20) AC(类似math.h中的sqrt(1.0*n),algorithm的abs()用于取绝对值)
    PAT:1013. Battle Over Cities (25) AC
    conv2的计算过程
    UFLDL Tutorial
    视频深度学习 Deep Learning
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11048905.html
Copyright © 2011-2022 走看看