// 15分代码
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b){return a > b;}
int to_int(int a[]){
int ans = 0;
for(int i = 0; i < 4; i++)
ans = ans * 10 + a[i];
return ans;
}
int main() {
int first[4]{0}, second[4]{0}, n, N[4]{0}, equal = true;
// 读取
cin >> n;
do{
int i = 0;
while(n){
N[i++] = n % 10;
n /= 10;
}
// 判断四位数字相同
for(int i = 0; i < 3; i++){
if(N[i] != N[i+1]){
continue;
}
equal = false;
}
if(equal == false){
for(auto n : N)
cout << n;
cout << " - ";
for(auto n : N)
cout << n;
cout << " = 0000";
exit(0);
}
sort(N, N + 4, cmp);
for(int i = 0; i < 4; i++)
first[i] = N[i];
for(auto it : first)
cout << it;
cout << " - ";
sort(N, N + 4);
for(int i = 0; i < 4; i++)
second[i] = N[i];
for(auto it : second)
cout << it;
// 按四位数格式输出
n = to_int(first) - to_int(second);
cout << " = " << n << endl;
}while(n != 6174);
}
// 18分代码
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b){return a > b;}
int to_int(int a[]){
int ans = 0;
for(int i = 0; i < 4; i++)
ans = ans * 10 + a[i];
return ans;
}
int main() {
int first[4]{0}, second[4]{0}, n, N[4]{0}, equal = true;
// 读取
cin >> n;
do{
int i = 0;
while(n){
N[i++] = n % 10;
n /= 10;
}
// 判断四位数字相同
if(N[0] == N[1] && N[1] == N[2] && N[2] == N[3]){
for(auto n : N)
cout << n;
cout << " - ";
for(auto n : N)
cout << n;
cout << " = 0000";
exit(0);
}
sort(N, N + 4, cmp);
for(int i = 0; i < 4; i++)
first[i] = N[i];
for(auto it : first)
cout << it;
cout << " - ";
sort(N, N + 4);
for(int i = 0; i < 4; i++)
second[i] = N[i];
for(auto it : second)
cout << it;
// 按四位数格式输出
n = to_int(first) - to_int(second);
cout << " = " << n << endl;
}while(n != 6174);
}
// 18分代码
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b){return a > b;}
int to_int(int a[]){
int ans = 0;
for(int i = 0; i < 4; i++)
ans = ans * 10 + a[i];
return ans;
}
int main() {
int first[4]{0}, second[4]{0}, n, N[4]{0}, equal = true;
// 读取
cin >> n;
do{
int i = 0;
while(n){
N[i++] = n % 10;
n /= 10;
}
sort(N, N + 4, cmp);
for(int i = 0; i < 4; i++)
first[i] = N[i];
for(auto it : first)
cout << it;
cout << " - ";
sort(N, N + 4);
for(int i = 0; i < 4; i++)
second[i] = N[i];
for(auto it : second)
cout << it;
// 按四位数格式输出
n = to_int(first) - to_int(second);
cout << " = ";
printf("%04d\n", n);
}while(n != 6174 && n != 0);
}
// 正确代码
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b){return a > b;}
int to_int(int a[]){
int ans = 0;
for(int i = 0; i < 4; i++)
ans = ans * 10 + a[i];
return ans;
}
int main() {
int n;
do{
int first[4]{0}, second[4]{0}, N[4]{0};
// 读取
cin >> n;
int i = 0;
while(n){
N[i++] = n % 10;
n /= 10;
}
sort(N, N + 4, cmp);
for(int i = 0; i < 4; i++)
first[i] = N[i];
for(auto it : first)
cout << it;
cout << " - ";
sort(N, N + 4);
for(int i = 0; i < 4; i++)
second[i] = N[i];
for(auto it : second)
cout << it;
// 按四位数格式输出
n = to_int(first) - to_int(second);
cout << " = ";
printf("%04d\n", n);
}while(n != 6174 && n != 0);
}
// 参考别人的正确代码
#include <bits/stdc++.h>
using namespace std;
int main() {
string ans;
cin >> ans;
ans.insert(0, 4 - ans.size(), '0');
do{
string first = ans, second = ans;
sort(begin(first), end(first), greater<int>());
sort(begin(second), end(second));
int n = stoi(first) - stoi(second);
ans = to_string(n);
ans.insert(0, 4 - ans.size(), '0');
cout << first << " - " << second << " = " << ans << endl;
}while(ans != "6174" && ans != "0000");
}
你的智商够不够高,是否考虑到所有的问题了?你可以想办法搞一份AC了的代码,里面写什么连看都不看,之后编写一个“测试数据制造机”,数大,数据多,情况就复杂,就可能出现你没想到的状况,拿两个程序对一下答案就知道了
当自己写的代码不知道有什么问题的时候,可以找一份完全正确的代码,然后编一点数据输入来对比两者输出的差别。(这里吐槽下牛客网的测试,比PAT上面的测试少了一种情况,以为算全部AC了,放PAT上结果是部分正确)
int first[4]{0}, second[4]{0}, N[4]{0};
a.insert(0, 4 - a.size(), '0');
测试输入有可能高位为0,比如01234、0001。这时候要么用代码高位补零,要么全部重置为0,然后按序填入非零数字。否则数组会携带上一次循环的其他数字而错。
还有重置需要放在循环里面,每次循环都重置。
一些其他问题。比如格式化输出的时候,printf比cout好用,如果这里结果得到三位有效数字123,按要求需要高位补零输出,那么输出时%04d,可以解决这个问题。
排序sort默认是小到大,非降序,即less<int>()。great<int>(),大到小,非升序。
当时编写代码的时候,同时考虑了四位数字相同和不相同时候的情况。所以刚开始的代码很长。后来写写发现,其实输出的时候可以合并处理的,不需要额外判断。所以以后编写代码的时候,应该先以最普遍情况考虑, 然后再考虑边界判断的问题。这样如果可以统一处理的,就不需要额外编写代码来判断了。