PAT 乙级 1019.数字黑洞 C++/Java

题目来源

给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。

例如,我们从6767开始,将得到

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...

现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。

输入格式:

输入给出一个 ( 区间内的正整数 N。

输出格式:

如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。

输入样例 1:

6767

输出样例 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

输入样例 2:

2222

输出样例 2:

2222 - 2222 = 0000

思路1:

用string接收输入,当数字不足四位数的时候,用0补高位

将字符串数字从高到低排序,再从低到高排序,分别转换成整型数字

得到的差值再转换成字符串,不足四位数高位补0,满足条件则退出循环。

需要注意的是,当差值为6174或者0000的时候要结束循环,当输入为6174的时候也要进行计算,所以这里用do while。

思路2:

用int接收输入,和思路1差不多,将输入转化成字符串,补0

然后做两次排序,转成整形数字再相减,得到差值

最用用%4d占位符,输出整形数字即可

 

C++实现:

思路1

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <map>
 6 #include <set>
 7 #include <string>
 8 #include <cctype>
 9 #include <unordered_map>
10 using namespace std;
11 
12 bool cmp(char a, char b)
13 {
14     return a > b;    //从高到低排序
15 }
16 
17 int main()
18 {
19     int result = 0;
20     string N;
21     cin >> N;
22     N.insert(0, 4 - N.size(), '0');
23     do 
24     {
25         string a = N;
26         string b = N;
27         sort(a.begin(), a.end(), cmp);
28         sort(b.begin(), b.end());
29         result = stoi(a) - stoi(b);
30         N = to_string(result);
31         N.insert(0, 4 - N.size(), '0');
32         cout << a << " - " << b << " = " << N << endl;
33     } while (N != "6174" && N != "0000");
34     return 0;
35 }

思路2

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <map>
 6 #include <set>
 7 #include <string>
 8 #include <cctype>
 9 #include <unordered_map>
10 using namespace std;
11 
12 bool cmp(char a, char b)
13 {
14     return a > b;
15 }
16 
17 int main()
18 {
19     int N;
20     int result = 0;
21     cin >> N;
22     string s = to_string(N);
23     do 
24     {
25         s.insert(0, 4 - s.size(), '0');
26         sort(s.begin(), s.end(), cmp);
27         int a = stoi(s);
28         sort(s.begin(), s.end());
29         int b = stoi(s);
30         result = a - b;
31         printf("%04d - %04d = %04d\n",a,b,result);
32         s = to_string(result);
33     } while (result != 0 && result != 6174);
34 
35     return 0;
36 }

 

 

 

Java实现:

 

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 
 4 //易错点:四位数输出
 5 public class Main {
 6     public static void main(String[] args) {
 7         Scanner in = new Scanner(System.in);
 8         int n = in.nextInt();
 9         in.close();
10         while (true) {  //两种情况跳出循环:1、4位数字相等;2、最终结果为6174
11             int x = BtoS(n);
12             int y = StoB(n);
13             n = x - y;
14             if (n == 0) {
15                 System.out.printf("%04d - %04d = %04d\n", x, y, n);
16                 break;
17             }
18             if (n == 6174){
19                 System.out.printf("%04d - %04d = %04d\n", x, y, n);
20                 break;
21             }
22             System.out.printf("%04d - %04d = %04d\n", x, y, n);
23         }
24     }
25 
26     public static int StoB(int n) {
27         char[] arr = String.format("%04d", n).toCharArray();    //.format():格式化
28         Arrays.sort(arr);                                       //数组排序
29         int temp = 0;
30         for (int i = 0; i < 4; i++) {
31             temp = temp * 10 + (arr[i] - '0');
32         }
33         return temp;
34     }
35 
36     public static int BtoS(int n) {
37         char[] arr = String.format("%04d", n).toCharArray();
38         Arrays.sort(arr);
39         int temp = 0;
40         for (int i = 3; i >= 0; i--) {
41             temp = temp * 10 + (arr[i] - '0');
42         }
43         return temp;
44     }
45 }

 

 

 

小结:

1. str.insert(0, 4 - s.size(), '0'); 是用来高位补0的,

 basic_string& insert( size_type index, size_type count, CharT ch ); 意思是,在index处插入count个字符ch

当字符串str为 22的时候,str.size() = 2, 所以在 index = 0处插入 4 - 2 = 2 个字符 '0'

这样就实现了高位补0

想了想,也不需要在高位补0,只要插入相应个数的0就可以了,毕竟需要对字符串进行排序,在哪里插入0都无所谓了

posted @ 2019-10-07 17:01  47的菠萝~  阅读(155)  评论(0编辑  收藏  举报