zoukankan      html  css  js  c++  java
  • 1023. Have Fun with Numbers

    1023. Have Fun with Numbers (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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
    import java.util.Scanner;
    
    public class Main {
    
      public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int init[]={0,0,0,0,0,0,0,0,0,0};
        String s=in.nextLine();
        int length = s.length();
        int[] str1 = new int[length];
        for(int i = 0; i < length; i++) {
          str1[i] = s.charAt(i) - '0';
        }
        int[] str2 =new int[21];
        int add=0,i=0, num=0,tmp=0;
        for (int k:str1){
            init[k]++;
          }
        for(int g=str1.length-1;g>=0;g--){
          tmp=str1[g];
          num=tmp*2+add;
          if(num>=10){
            str2[i++]=num%10;
            add=num/10;
          }
          else{
            str2[i++]=num;
            add=0;
          }
        }
        if(add!=0){
          str2[i++]=add;
        }
        for(int k=i-1;k>=0;k--){
          init[str2[k]]--;
        }
        int flag=0;
        for (int k=0;k<10;k++){
          if(init[k]!=0) {
            flag=1;
            break;
          }
        }
        if(flag==1) System.out.println("No");
        else System.out.println("Yes");
        for (int k=i-1;k>=0;k--){
          System.out.print(str2[k]);
        }
        System.out.println();
      }
    
    }

    这题要注意的是:

    1.最高位如果有进位不要漏掉;

    2.字符串的处理吧,之前写的代码用的split一直通过不了,不知道为啥在同学的机子上跑出来的话最高位会缺失,但是我自己的就正常可能是这里有些问题吧,不懂,希望之后能想明白orz

    错误的代码如下

    import java.util.Scanner;
    
    public class Main {
    
      public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int init[]={0,0,0,0,0,0,0,0,0,0};
        String s=in.nextLine();
        String str1[]=s.split("");
        int[] str2 =new int[21];
        int add=0,i=0, num=0,tmp=0;
        for (String k:str1){
          init[Integer.parseInt(k)]++;
        }
        for(int g=str1.length-1;g>=0;g--){
          tmp=Integer.parseInt(str1[g]);
          num=tmp*2+add;
          if(num>=10){
            str2[i++]=num%10;
            add=num/10;
          }
          else{
            str2[i++]=num;
            add=0;
          }
        }
        if(add!=0){
          str2[i++]=add;
        }
        for(int k=i-1;k>=0;k--){
          init[str2[k]]--;
        }
        int flag=0;
        for (int k=0;k<10;k++){
          if(init[k]!=0) {
            flag=1;
            break;
          }
        }
        if(flag==1) System.out.println("No");
        else System.out.println("Yes");
        for (int k=i-1;k>=0;k--){
          System.out.print(str2[k]);
        }
        System.out.println();
      }
    
    }
  • 相关阅读:
    Sicily shortest path in unweighted graph
    Sicily connect components in undirected graph
    Sicily 1931. 卡片游戏
    Sicily 1021. Couples
    c++ 高效文本读写
    Sicily 1129. ISBN
    Sicily 1133. SPAM
    Sicily 1282. Computer Game
    小工具?不,这是小工具的集合!
    .Net Core建站(4):FTP发布项目及连接服务器数据库
  • 原文地址:https://www.cnblogs.com/chen20135517/p/7731639.html
Copyright © 2011-2022 走看看