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 }
  • 相关阅读:
    documentFragment文档碎片
    OpenResty之resty.limit.count 模块介绍
    vue前端分页多条件搜索
    element ui Tree树形控件获取未全选父节点和子节点id
    如何使 pdf 文件在浏览器里面直接下载而不是打开
    关于本博客
    圆锥曲线基础知识点
    NOI2021游记
    20210716模拟赛
    计数+动态规划
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11048905.html
Copyright © 2011-2022 走看看