1019 Digital Black Hole (20 minutes)

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <stdlib.h>
#include <iostream>

using namespace std;

int main ()
{
    int N,h,l,i,j;
    char num[5],num1[5];
    char tmp;
    cin>>N;
    do
    {
        sprintf(num,"%d",N);
        if(strlen(num)==3)
            num[3]='0';
        else if(strlen(num)==2)
        {
            a [ 3 ] = ' 0 ' ;
            a [ 2 ] = ' 0 ' ;
        }
        else if(strlen(num)==1)
        {
            a [ 3 ] = ' 0 ' ;
            a [ 2 ] = ' 0 ' ;
            a [ 1 ] = ' 0 ' ;
        }
        for(i=0;i<3;i++)
        {
            for(j=0;j<3-i;j++)
            {
                if(num[j]<num[j+1])
                {
                    tmp=num[j];
                    num [h] = num [H + 1 ];
                    num[j+1]=tmp;
                }
            }
        }
        num1 [ 0 ] = a [ 3 ];
        num1 [ 1 ] = a [ 2 ];
        num1 [ 2 ] = a [ 1 ];
        num1 [ 3 ] = a [ 0 ];
        h = atoi (num);
        l = atoi (num1);
        N=h-l;
        printf("%04d - %04d = %04d\n",h,l,N);
        }while(N!=6174&&N!=0);
    return 0;
}

The above codes can be correctly

The following statements using cout + three samples can not judge by, I do not know why is it wrong.

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <stdlib.h>
#include <iostream>

using namespace std;

int main ()
{
    int N,h,l,i,j;
    char num[5],num1[5];
    char tmp;
    cin>>N;
    do
    {
        sprintf(num,"%d",N);
        if(strlen(num)==3)
            num[3]='0';
        else if(strlen(num)==2)
        {
            a [ 3 ] = ' 0 ' ;
            a [ 2 ] = ' 0 ' ;
        }
        else if(strlen(num)==1)
        {
            a [ 3 ] = ' 0 ' ;
            a [ 2 ] = ' 0 ' ;
            a [ 1 ] = ' 0 ' ;
        }
        for(i=0;i<3;i++)
        {
            for(j=0;j<3-i;j++)
            {
                if(num[j]<num[j+1])
                {
                    tmp=num[j];
                    num [h] = num [H + 1 ];
                    num[j+1]=tmp;
                }
            }
        }
        num1 [ 0 ] = a [ 3 ];
        num1 [ 1 ] = a [ 2 ];
        num1 [ 2 ] = a [ 1 ];
        num1 [ 3 ] = a [ 0 ];
        h = atoi (num);
        l = atoi (num1);
        N=h-l;
        //printf("%04d - %04d = %04d\n",h,l,N);
        cout<<num<<" - "<<num1<<" = ";
        if(N<1000&&N>=100)
            cout<<"0"<<N<<endl;
        else if(N<100&&N>=10)
            cout<<"00"<<N<<endl;
        else if(N<10)
            cout<<"000"<<N<<endl;
        else
            cout<<N<<endl;
            
        }while(N!=6174&&N!=0);
    return 0;
}

Summary: Formatting output using printf

Guess you like

Origin www.cnblogs.com/QRain/p/12228321.html