1069. The Black Hole of Numbers (20)
来源:互联网 发布:windows邮件客户端 编辑:程序博客网 时间:2024/06/05 10:43
注意保证始终是4位
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int str2num(string s){int sum = 0;for(auto& c : s){sum *= 10;sum += c - '0';}return sum;}string num2str(int n){string s;do{s = char(n%10 + '0') + s;n /= 10;}while(n);while(s.size() < 4) s = "0"+s;return s;}int main(){string s;cin >> s;while(s.size() < 4) s = "0"+s;string rs(s);sort(begin(s), end(s));sort(rs.rbegin(), rs.rend());int first = str2num(rs), second = str2num(s);int diff = first - second;bool zero = false;while(diff != 6174){if(diff == 0){printf("%04d - %04d = %04d\n", first, second, diff);zero = true;break;}printf("%04d - %04d = %04d\n", first, second, diff);rs = s = num2str(diff);sort(begin(s), end(s));sort(rs.rbegin(), rs.rend());first = str2num(rs);second = str2num(s);diff = first - second;}if(!zero){printf("%04d - %04d = %04d\n", first, second, diff);}return 0;}
0 0
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- C语言——内存管理
- dede arclist ajax分页
- ActionBar的Overlay模式如何不遮盖顶部内容的问题
- 经典递归问题集锦
- 优质软件及资源整理-Kylin推荐
- 1069. The Black Hole of Numbers (20)
- 数据结构算法之排序系列Java、C源码实现(6)--快速排序
- 2016.2.27模拟比赛总结
- [HNOI2008] [BZOJ1009] GT考试
- 第六章:Linux的文件权限
- NDK开发环境的搭建
- 智能硬件X1000平台代码下载编译方法
- ytu 2228 判断链表
- [leetcode Q26] Remove Duplicates from Sorted Array