-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
99 lines (84 loc) · 1.42 KB
/
main.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
#include <iostream>
#include <math.h>
#include <ctype.h>
#include <thread>
#include "rlutil.h"
using namespace std;
int freq[10], num_digits;
void gen_nums(int curr_place, unsigned long long curr_num = 0);
void esc_exit();
int main(void)
{
for(int i = 0; i < 10; i++)
{
freq[i] = 0;
}
int a;
while(true)
{
cin >> a;
if(a <= 9 && a >= 0)
{
freq[a]++;
}
else if(a == -1)
{
break;
}
else
{
cout << "\nInput Skipped - " << a << endl;
}
}
int total_digits = 0;
cout << endl;
for(int i = 0; i < 10; i++)
{
total_digits += freq[i];
if(freq[i] != 0)
{
cout << i << ' ';
}
}
cout << endl;
cout << "Total Number of digits: " << total_digits << endl;
do
{
cout << "Enter the number of digits for which numbers are to be generated: ";
cin >> num_digits;
}
while(num_digits > total_digits || num_digits < 0);
std::thread p = std::thread(esc_exit);
gen_nums(num_digits);
cout << endl;
}
void gen_nums(int curr_place, unsigned long long curr_num)
{
if(curr_place == 0)
{
cout << curr_num << "\n";
return;
}
for(int i = (curr_place == num_digits ? 1 : 0); i < 10; i++)
{
if(freq[i] != 0)
{
freq[i]--;
unsigned long long temp = curr_num + i * pow(10, curr_place - 1);
gen_nums(curr_place - 1, temp);
freq[i]++;
}
}
}
void esc_exit()
{
std::cin.ignore(1000, '\n');
while(true)
{
if(kbhit())
{
std::cout.flush();
exit(1);
}
}
}