您的位置:首页 > 其它

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

2015-06-09 15:13 183 查看


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

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;
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: