6174

Study/C++ 2011. 9. 29. 20:43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
 
using namespace std;
 
void main()
{
    char input[4];
     
    int temp=0;
    int i,j;
     
    int max_number=0;
    int min_number=0;
     
    int result=0;
    int count=0;
     
    for(i=0;i<4;i++)
    {
        cin>>input[i];
    }
     
    while(result != 6174)
    {
        for(i=0;i<4;i++)
        {
            for(j=i;j<4;j++)
            {
                if(input[i] < input[j])
                {              
                    temp = input[i];
                    input[i] = input[j];
                    input[j] = temp;
                }
                 
                max_number = (int)input[0] * 1000 + (int)input[1] * 100 + (int)input[2] * 10 + (int)input[3];          
                 
                if(input[i] > input[j])
                {              
                    temp = input[i];
                    input[i] = input[j];
                    input[j] = temp;
                }
                 
                min_number = (int)input[3] * 1000 + (int)input[2] * 100 + (int)input[1] * 10 + (int)input[0];
                 
                result = (min_number)-(max_number);
            }
        }
        cout << min_number << " - " << max_number << " = " << result << endl;
         
        int temp = result;
         
        for(int i=0; i<4; i++)
        {
            input[i] = temp % 10;
            temp /= 10;
        }
    }
}

'Study > C++' 카테고리의 다른 글

구조체 성적계산하기  (0) 2011.09.29
Hangover1003  (0) 2011.09.29
토크나이저 - 문제해결기법 튜터링  (0) 2011.09.29
볼링게임  (0) 2011.09.29
스택과 큐  (0) 2011.09.29
Posted by 코딩하는 야구쟁이
,