1019. 数字黑洞 (20)

来源:互联网 发布:图片相似度比较算法 编辑:程序博客网 时间:2024/05/10 14:47

挺好玩的一道题目,作为字符串处理的时候,一定要考虑好输入的不是4位数字的情况

// 2017/10/14 NCU // PAT-B 1053// scienceZ#include <cstdio>#include <cstring>#include <stack>#include <iostream>#include <algorithm>using namespace std;char arr[10];int n, a, b, key = 1;bool my(char a, char b){    return a>b;}int main(){           cin >> n;    sprintf(arr, "%04d", n);    while(key){        sort(arr, arr+4, my);        a = atoi(arr);        sort(arr, arr+4);        b = atoi(arr);        if (a-b == 6174 || a-b == 0) key = 0;        printf("%04d - %04d = %04d\n", a, b, a-b);        sprintf(arr, "%04d", a-b);    }       return 0;}