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 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.
import java.math.BigInteger;
import java.util.*;
public class pat1023 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
BigInteger number = scan.nextBigInteger();//输入原始数据
BigInteger result = number.multiply(BigInteger.valueOf(2));//将这个数据乘2
ArrayList<Character>list1=new ArrayList();//创建两个char类型的列表(list就是一个动态大小的数组)
ArrayList<Character>list2=new ArrayList();
sortOfNumber(list1,number);//进入方法
sortOfNumber(list2,result);
if(list1.equals(list2))System.out.println("Yes");//如果两个排序后的列表相等则为yes
else System.out.println("No");//否则
System.out.println(result);
}
private static void sortOfNumber(ArrayList<Character>list,BigInteger number) {
String s=number.toString();//先将输入的number转换为字符串类型
char[] ch=s.toCharArray();//将字符串按字符型输入进ch数组
for(char c:ch) {
list.add(c);//增强for遍历数组char c就是类型加一个临时变量,然后add进list列表中
}
Collections.sort(list);//跳过了列表大小的问题,然后直接排序列表
}
}