PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟

博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/6789244.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~~

 

按照题目的意思纯模拟一遍就行了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn=10;
char str[maxn],str1[maxn],str2[maxn];
int a,b,c;
bool cmp1(char a,char b){
    return a>b;
}
bool cmp2(char a,char b){
    return a<b;
}
int main()
{
    int n;
    scanf("%d",&n);
    sprintf(str,"%04d",n); //输入的n可能小于4位数,要补0
    bool isSame=true;
    for(int i=1;i<4;i++){
        if(str[i]!=str[i-1]){
            isSame=false;
        }
    }
    if(isSame){
        printf("%s - %s = 0000\n",str,str);
    }
    else{
        c=0;
        while(c!=6174){
            //strcpy(str1,str);
            //strcpy(str2,str);
            sort(str,str+4,cmp1);
            a=atoi(str);
            sort(str,str+4,cmp2);
            b=atoi(str);
            c=a-b;
            printf("%04d - %04d = %04d\n",a,b,c);
            sprintf(str,"%04d",c); //注意填0
        }
    }
    return 0;
}
View Code

 

posted @ 2017-04-30 12:45  辰曦~文若  阅读(209)  评论(0编辑  收藏  举报