-
Notifications
You must be signed in to change notification settings - Fork 4
/
recreateHighestTime.cpp
146 lines (124 loc) · 2.01 KB
/
recreateHighestTime.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
The user is inserting 8 numbers, from thouse numbers you should recreate the higgest time possible,
where 23:59:59 is the higest and 00:00:00 is the lowest.
The output of the time should be hh:mm:ss.
*/
#include <iostream>
using namespace std;
class Time
{
public:
int hours;
int minutes;
int seconds;
void Print()
{
if (hours < 10)
cout << '0';
cout << hours << ":";
if (minutes < 10)
cout << '0';
cout << minutes << ":";
if (seconds < 10)
cout << '0';
cout << seconds << endl;
}
Time(int hours = 23, int minutes = 59, int seconds = 59)
: hours(hours), minutes(minutes), seconds(seconds)
{}
void operator--()
{
if (seconds > 0)
seconds--;
else
{
seconds = 59;
if (minutes > 0)
minutes--;
else
{
minutes = 59;
if (hours > 0)
hours--;
else
hours = 23;
}
}
}
bool operator==(Time t) const
{
return t.hours == hours && t.minutes == minutes && t.seconds == seconds;
}
bool operator!=(Time t) const
{
return !(t == *this);
}
};
bool recreateHighestTime(int arr[], int n, Time &t)
{
int temp[10] = { 0 };
for (int i = 0; i < 10; i++)
temp[i] = arr[i];
Time x(0, 0, 0);
int c;
bool test = false;
while (t != x)
{
test = false;
c = t.hours % 10;
if (arr[c] >= 1)
arr[c]--;
else
test = true;
c = t.hours / 10;
if (arr[c] >= 1)
arr[c]--;
else
test = true;
c = t.minutes % 10;
if (arr[c] >= 1)
arr[c]--;
else
test = true;
c = t.minutes / 10;
if (arr[c] >= 1)
arr[c]--;
else
test = true;
c = t.seconds % 10;
if (arr[c] >= 1)
arr[c]--;
else
test = true;
c = t.seconds / 10;
if (arr[c] >= 1)
arr[c]--;
else
test = true;
if (!test)
{
t.Print();
return true;
}
--t;
for (int i = 0; i < 10; i++)
arr[i] = temp[i];
}
t.Print();
return false;
}
int main()
{
int arr[10];
for (int i = 0; i < 10; i++)
arr[i] = 0;
int i = 0, input;
do {
cin >> input;
arr[input]++;
i++;
} while (i < 8);
Time t;
createTime(arr, 9, t);
system("PAUSE>0");
}