zoukankan      html  css  js  c++  java
  • 00-自测4. 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 file 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 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 
     5 public class number
     6 {
     7     public static void main(String[] args) throws IOException
     8     {
     9         BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
    10         String line=buf.readLine();
    11         char[] sou=line.toCharArray();
    12         char[] dou;
    13         boolean b=false;
    14         if(sou[0]-'0'>=5)
    15         {
    16             b=false;
    17             dou=new char[sou.length+1];
    18             dou[0]='1';
    19             for(int i=sou.length-1,jin=0;i>=0;i--)
    20             {
    21                 dou[i+1]=(char) (((sou[i]-'0')*2)%10+'0'+jin);
    22                 if((sou[i]-'0')*2+jin>=10)
    23                     jin=1;
    24                 else
    25                     jin=0;
    26                 
    27             }
    28         }
    29         else
    30         {
    31             dou=new char[sou.length];
    32             for(int i=sou.length-1,jin=0;i>=0;i--)
    33             {
    34                 
    35                 dou[i]=(char) (((sou[i]-'0')*2)%10+'0'+jin);
    36                 if((sou[i]-'0')*2+jin>=10)
    37                     jin=1;
    38                 else
    39                     jin=0;
    40                 
    41             }
    42             
    43             int[] t1=new int[10];
    44             for(int i=0;i<sou.length;i++)
    45             {
    46                 t1[sou[i]-'0']++;
    47             }
    48             int[] t2=new int[10];
    49             for(int i=0;i<dou.length;i++)
    50             {
    51                 t2[dou[i]-'0']++;
    52             }
    53             for(int i=0;i<t1.length;i++)
    54             {
    55                 if(t1[i]!=t2[i])
    56                 {
    57                     b=false;
    58                     break;
    59                 }
    60                 b=true;
    61             }
    62         }
    63 
    64         if(b==true)
    65         {
    66             System.out.println("Yes");
    67             System.out.println(dou);
    68         }
    69         else
    70         {
    71             System.out.println("No");
    72             System.out.println(dou);
    73         }
    74             
    75         
    76     }
    77 }
  • 相关阅读:
    第三次博客园作业
    centos7+jdk1.8+tomcat8 配置https
    输入30个数存入数组a,求出数的每个位数的平方和存入数组b,从小到大排列后输出(C语言)
    50个[100,300]的随机数,要求用二分法查找从键盘录入的关键数字。找到回复位置,找不到回复不存在(C语言)
    产生20个随机数,在[200,400]内,其中能被5整除的存入数组array2,要求输出array2中的平均值(C语言)
    最小生成树
    PTA路径判断
    PTA构造哈夫曼树
    图的其中两种表示方式
    中序遍历树并判断是否为二叉搜索树
  • 原文地址:https://www.cnblogs.com/qq1029579233/p/4398580.html
Copyright © 2011-2022 走看看