00-自测4. Have Fun with Numbers

来源:互联网 发布:海岛大亨5 mac 汉化 编辑:程序博客网 时间:2024/05/17 00:03

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

实现代码(通过测试):

package com.PATDataStruct;/* * 通过测试 */import java.util.ArrayList;import java.util.Iterator;import java.util.Scanner;public class SelfTest04_1 {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);String str = input.nextLine();char ch[] = str.toCharArray();input.close();//double newN = 2*N;//System.out.println(N);//System.out.println(newN);ArrayList<Integer> al = change2Num(ch);ArrayList<Integer> newAl = double2(al);//ArrayList<Integer> al2 = newAl;//traversal(newAl);if (al.size() != newAl.size() || !compare(al, newAl)){System.out.println("No");traversal(double2(change2Num(ch)));//System.out.println(2*(long)N);}else {System.out.println("Yes");traversal(double2(change2Num(ch)));//System.out.println(2*(long)N);}}public static ArrayList<Integer> double2(ArrayList<Integer> al){int rem = 0;int temp = 0;ArrayList<Integer> newAl = new ArrayList<Integer>();for (int i = al.size()-1; i >= 0; i--){//System.out.println(11111);rem = (al.get(i)*2) % 10;//余数newAl.add(rem+temp);temp = (al.get(i)*2) / 10;//进位的数字}//判断是否比原始数字多出一位if (temp != 0){newAl.add(temp);}return newAl;}//将字符数组转换成数字数组public static ArrayList<Integer> change2Num(char ch[]){ArrayList<Integer> al = new ArrayList<Integer>();for (int i = 0; i < ch.length; i++){al.add(Character.getNumericValue(ch[i]));//System.out.println(Character.getNumericValue(ch[i]));}//traversal(al);return al;}public static boolean compare(ArrayList<Integer> newAl, ArrayList<Integer> al){al = BubbleSort(al);//traversal(al);newAl = BubbleSort(newAl);//traversal(newAl);Iterator<Integer> ital = al.iterator();Iterator<Integer> itNewAl = newAl.iterator();while(ital.hasNext()){if (ital.next() != itNewAl.next()){return false;}}return true;}//public static ArrayList<Integer> orderNum(double N){//ArrayList<Integer> al = new ArrayList<Integer>();//while (true){//double temp = N % 10;//al.add((int)temp);//N = (N - temp)/10;//if (N == 0){//break;//}//}////System.out.println(al.size());//return al;//}public static ArrayList<Integer> BubbleSort(ArrayList<Integer> al) {int temp = 0;for (int i = 0; i < al.size(); i++){for (int j = i+1; j < al.size(); j++){/* * 从小到大排序 */if (al.get(j) < al.get(i)){temp = al.get(j);al.set(j, al.get(i));al.set(i, temp);}}}return al;}public static void traversal(ArrayList<Integer> al){//Iterator<Integer> it = al.iterator();//while(it.hasNext()){//System.out.print(it.next());//}for (int i = al.size()-1; i >= 0; i--){System.out.print(al.get(i));}System.out.println();}}


0 0
原创粉丝点击