自测4. Have Fun with Numbers (20)

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


1、 可以考虑使用数组存储每个数字,然后比较两个数组是否相等来判断。比较数组的时候,可以先调用排序函数排序后直接比较。
2、 可以使用一个标记数组,数组大小为10,代表0-9十个数字,数组的内容为数字出现的次数,比较两个数组是否相等即可,这样可以免去排序的过程。

PS: 题目中说明输入的字符不超过20位,但是java中 ,最长的long也是有64位,只能表示18位数字,因此这里需要通过字符串来解决输入问题。

import java.util.Arrays;
import java.util.Scanner;




public class Main2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //读入数字并转化为数字数组
        Scanner in = new Scanner(System.in);
        String numsString = in.nextLine();
        char[] numchar = numsString.toCharArray();
        int[] num = new int[numchar.length];
        for (int i = 0; i < num.length; i++) {
            num[i]= Integer.parseInt(String.valueOf(numchar[i]));
        }

        //标记初始数字的0~9出现的次数
        int[] flag = new int[10];
        for(int i = 0; i<num.length;i++){
            flag[num[i]]++;
        }

//      for (int i : num) {
//          System.out.print(i+" ");
//      }
        //对数字乘以2
        int carry = 0;
        int[] num2 = new int[num.length];
        for (int i = num.length-1; i >=0; i--) {
            int temp = 2*num[i]+carry;
            num2[i] = temp%10;
            carry = temp/10;            
        }




        //标记翻倍后的数字
        int[] flag2 = new int[10];
        if(num2.length>2){
        for(int i = 0;i<num2.length;i++){
            flag2[num2[i]]++;
        }
        }
        //输出结果
        if (Arrays.equals(flag, flag2)) {
            System.out.println("Yes");
            for (int i : num2) {

                    System.out.print(i);

            }

        }else {
            System.out.println("No");
            if (carry!=0) {
                System.out.print(carry);  //对于某些翻倍后有进位的情况,需要把进位值先输出
            }
            for (int i : num2) {


                    System.out.print(i);

            }
        }



}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值