1069 The Black Hole of Numbers 解题代码测试结果问题整理 解题代码 #include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; bool cmp(char a, char b) { return a > b; } int main() { string a, b; cin >> a; do { a.insert(0, 4 - a.length(), '0'); b = a; sort(a.begin(), a.end(), cmp); sort(b.begin(), b.end()); printf("%04d - %04d = %04d\n", stoi(a), stoi(b), stoi(a) - stoi(b)); a = to_string(stoi(a) - stoi(b)); } while (stoi(a)!=0 && stoi(a)!=6174); return 0; } 测试结果 问题整理 1.简单数学。 2.用string的to_string() insert() stoi() 函数代替sprintf sscanf 3.sprintf中对于补零操作不友好,因为结尾\0存在的因素。