• 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();
      }
    
    }
  • 相关阅读:
    51nod 1179 最大的最大公约数 (数论)
    POJ 3685 二分套二分
    POJ 3045 贪心
    LIC
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    HDU 2389 Rain on your Parade
    HDU 2819 Swap
    HDU 1281 棋盘游戏
    HDU 1083 Courses
  • 原文地址:https://www.cnblogs.com/chen20135517/p/7731639.html
Copyright © 2020-2023  润新知