【PAT 乙级(Basic Level)】数字黑洞


代码质量不高,不过目的实现了。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        int[] i = new int[4];
        i[0] = t / 1000;
        i[1] = t / 100 % 10;
        i[2] = t / 10 % 100 % 10;
        i[3] = t % 1000 % 100 % 10;

        while (true) {
            int[] unsortedSmall = i;
            int[] unsortedLarge = i;
            for (int sIndexI = 0; sIndexI < unsortedSmall.length; sIndexI++) {
                for (int sIndexJ = sIndexI; sIndexJ < unsortedSmall.length; sIndexJ++) {
                    if (unsortedSmall[sIndexI] > unsortedSmall[sIndexJ]) {
                        int temp = unsortedSmall[sIndexI];
                        unsortedSmall[sIndexI] = unsortedSmall[sIndexJ];
                        unsortedSmall[sIndexJ] = temp;
                    }
                }
            }
            int small = unsortedSmall[0] * 1000 + unsortedSmall[1] * 100 + unsortedSmall[2] * 10 + unsortedSmall[3];
            String smallStr = unsortedSmall[0] + "" + unsortedSmall[1] + "" + unsortedSmall[2] + "" + unsortedSmall[3];

            for (int lIndexI = 0; lIndexI < unsortedLarge.length; lIndexI++) {
                for (int lIndexJ = lIndexI; lIndexJ < unsortedLarge.length; lIndexJ++) {
                    if (unsortedLarge[lIndexI] < unsortedLarge[lIndexJ]) {
                        int temp = unsortedLarge[lIndexI];
                        unsortedLarge[lIndexI] = unsortedLarge[lIndexJ];
                        unsortedLarge[lIndexJ] = temp;
                    }
                }
            }
            int large = unsortedLarge[0] * 1000 + unsortedLarge[1] * 100 + unsortedLarge[2] * 10 + unsortedLarge[3];
            String largeStr = unsortedLarge[0] + "" + unsortedLarge[1] + "" + unsortedLarge[2] + "" + unsortedLarge[3];

            int differential = large - small;
            if (large == small) {
                System.out.println(largeStr + " - " + smallStr + " = " + "0000");
                break;
            } else {
                System.out.println(largeStr + " - " + smallStr + " = " + differential);
                if (differential - 6174 == 0) {
                    break;
                } else {
                    i = change(differential);
                }
            }
        }
    }

    private static int[] change(int t) {
        int[] i = new int[4];
        i[0] = t / 1000;
        i[1] = t / 100 % 10;
        i[2] = t / 10 % 100 % 10;
        i[3] = t % 1000 % 100 % 10;
        return i;
    }
}

以前没过的记录就不删了,纪念一下傻乎乎的过去。


怒啊!不管怎么样都有一个测试点过不去,问题是我想到的例子都尝试了。知道自己有错,却不知道自己错在哪里,完全糟心。


无论如何,本题还是有收获的:

①本身题目要求是四位数,最开始我是用队列,后来发现长度固定,我应该用数组,多么痛的领悟。

②补零。当我测试1000时,第一个等式的答案是999,我以为我找到了测试点4的问题,好吧,后来证明不是这个问题。但是我仍旧尝试在999前面补零。

我参考了这篇文章:http://blog.csdn.net/getdate/article/details/4558411 侵立删

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		StringBuffer before = new StringBuffer();
		StringBuffer after = new StringBuffer();
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int[] a = new int[4];
		while (true) {
			a[0] = n / 1000;
			a[1] = n / 100 % 10;
			a[2] = n / 10 % 10;
			a[3] = n % 10;
			int temp = 0;
			if (a[0] == a[1] && a[0] == a[2] && a[0] == a[3] ) {
				System.out.println(n + " - " + n + " = " + 0);
				break;
			} else {
				for (int i = 0; i < 3; i++) {
					for (int j = 1; j < 4; j++) {
						if (i < j && a[i] < a[j]) {
							temp = a[j];
							a[j] = a[i];
							a[i] = temp;
						}
					}
				}
				before.append(a[0] + "" + a[1] + "" + a[2] + "" + a[3]);
				for (int i = 0; i < 3; i++) {
					for (int j = 1; j < 4; j++) {
						if (i < j && a[i] > a[j]) {
							temp = a[j];
							a[j] = a[i];
							a[i] = temp;
						}
					}
				}
				after.append(a[0] + "" + a[1] + "" + a[2] + "" + a[3]);
				int result = Integer.parseInt(before.toString())
						- Integer.parseInt(after.toString());
				if(result<1000){
					String str=String.format("%04d", result);
					System.out.println(before + " - " + after + " = " +str );
				}else{
					System.out.println(before + " - " + after + " = " +result );
				}
				n = result;
				before.delete(0, 4);
				after.delete(0, 4);
				if (n == 6174) {
					break;
				}
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值