1. 程式人生 > >1023. Have Fun with Numbers

1023. Have Fun with Numbers

for each stand test case pro 時間 cas 內存 app ring

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();
  }

}

1023. Have Fun with Numbers