标签:
输入给出一个(0, 10000)区间内的正整数N。
如果N的4位数字全相等,则在一行内输出“N - N = 0000”;否则将计算的每一步在一行内输出,直到6174作为差出现,输出格式见样例。注意每个数字按4位数格
式输出。
6767
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
import java.util.*; import java.text.DecimalFormat;; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int A; DecimalFormat format = new DecimalFormat("0000"); while(in.hasNext()){ A = in.nextInt(); while(true){ if(A == 6174){ break; } if(A==1111 || A==2222 ||A==3333 ||A==4444 ||A==5555 ||A==6666|| A==7777 ||A==8888 ||A==999 ){ System.out.println(A +" - "+A+" = 0000"); break; } int[] res = change(A); int x = res[1] - res[0]; System.out.println(format.format(res[1]) + " - " +format.format(res[0]) +" = " +format.format(x) ); A = x; if(x<=0) break; } } } public static int[] change(int A){ int[] nums = new int[10]; int[] res = new int[2]; while(A>=1){ nums[A%10]++; A=A/10; } int sum = 0; for(int a:nums) sum+=a; int B = 0; int C = 0; for(int i=0;i<10;i++){ int count1 = nums[i]; while(count1!=0){ B = B*10 + i; count1--; } int count2 = nums[10-i-1]; while(count2!=0){ C = C*10 + 10-i-1; count2--; } } while(C<1000){ C = C * 10; } res[0] = B; res[1] = C; return res; } }
标签:
原文地址:http://www.cnblogs.com/theskulls/p/5724218.html