【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; } } } } }
相关文章推荐
- hibernate执行sql优化
- linux基本命令<2>
- [ 转 ] 一个平庸程序员的自白
- uchome各个功能模块对应模块说明
- Fresco 如何在Android Studio中编译
- 黑马程序员——java反射基础总结
- 高度自适应
- SAT写作全部真题+OG+OC完整版(2)
- 在win7(32)以及xp上python环境的搭建
- python学习之旅(一)-----字典
- 单链表
- 数据透视表中巧用平均值计算及时妥投率
- C#基础-----抽象类实现多态
- 黑马程序员---集合-Map集合,Collections类
- java 线程中 stop()和 suspend()方法为何不推荐使用?
- Linux C编程从入门到精通——Linux基础
- Onload,Onunload,onbeforeunload,$(window).load(function() {})和$(document).ready(function(){})
- COM学习笔记1_COM初步探索
- 4Sum
- 项目管理 01