[PAT][Basic Level]1019 done

AC CODE:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int ArrayToNumber(char *ch);
char * NumberToArray(int  number);
bool cmp(char a,char b)
{
    return a > b;
}
int main()
{
    char *n ;
    n = (char *)malloc(sizeof(char)*5);
    int num;
    cin>>num;
    n = NumberToArray(num);
    //char *p = n ;
    
    int num1,num2,temp;
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]) {
        cout<<n<<" - "<<n<<" = "<<"0000"<<endl;
        return 0;
    }
  
 while(temp!=6174){
    sort(n,n+4,cmp);
    cout<<n<<" - ";
    num1 = ArrayToNumber(n);
    sort(n,n+4);
    cout<<n<<" = ";
    num2 = ArrayToNumber(n);
    
    temp = num1 - num2 ;
  // cout<<temp<<endl;
    n = NumberToArray(temp);
    //cout<<p[0]<<p[1]<<p[2]<<p[3]<<endl;
    cout<<n<<endl;
    
    
 }   
    
    
    return 0 ;
}
int ArrayToNumber(char *ch){
   int result = 0;
   result = ch[0]*1000+ch[1]*100+ch[2]*10+ch[3];
   return result;
}
char * NumberToArray(int  number){
    char * m;
    m =(char *)malloc(sizeof(char)*5);
    
    m[0] = number/1000%10 +'0' ;//qian
    m[1] = number/100%10 + '0';//bai 
    m[2] = number/10%10 + '0';//shi
    m[3] = number%10 + '0';//ge
    //cout<<m[0]<<m[1]<<m[2]<<m[3]<<endl;
    return m;
} 
上一篇:笔记11.29


下一篇:shell循环体内变量传递无效