The next statement is the problem for an exercise.
I did this solution. It's executing a loop from 0 to 10^9. But it's taking many time for being executed. So, the next step which I thought is add a hashmap into the result between the number and its reverse as keys, saving the result.
But the program is still consuming time. I guess I'm missing maybe a math theory or another approach to solve time in less time.
Program:
#include <iostream>
#include <cmath>
using namespace std;
int reverseNumber(int n) {
int reverse = 0;
while (n > 0) {
reverse = reverse * 10;
reverse += n % 10;
n = n / 10;
}
return reverse;
}
string isNumberEvenOddOrMixed(int n) {
bool even = false;
bool odd = false;
while (n > 0) {
int rem = n % 10;
n = n / 10;
if (rem % 2 == 0)
even = true;
else
odd = true;
}
return even && odd ? "Mixed" : (even ? "Even" : "Odd");
}
int main(int argc, const char * argv[]) {
int even = 0, odd = 0, mixed = 0;
for (int i = 0; i < pow(10, 9); i++) {
// Verify if i already exists in the hashmap,
// if it is, just print the value saved.
int nReversed = reverseNumber(i);
int sum = i + nReversed;
string result = isNumberEvenOddOrMixed(sum);
if (result == "Even") even++;
else if (result == "Odd") odd++;
else mixed++;
// Save i and nReversed in a hashmap with the result
// hashmap[i] = result;
// hashmap[nReversed] = result;
cout << i << " + " << nReversed << " = " << sum << " : " << result << "n";
}
cout << "Even: " << even << "n";
cout << "Odd: " << odd << "n";
cout << "Mixed: " << mixed << "n";
return 0;
}
These are the results from 0 to 10^2. I just printed to see the relationship between the numbers:
0 + 0 = 0 : Odd
1 + 1 = 2 : Even
2 + 2 = 4 : Even
3 + 3 = 6 : Even
4 + 4 = 8 : Even
5 + 5 = 10 : Mixed
6 + 6 = 12 : Mixed
7 + 7 = 14 : Mixed
8 + 8 = 16 : Mixed
9 + 9 = 18 : Mixed
10 + 1 = 11 : Odd
11 + 11 = 22 : Even
12 + 21 = 33 : Odd
13 + 31 = 44 : Even
14 + 41 = 55 : Odd
15 + 51 = 66 : Even
16 + 61 = 77 : Odd
17 + 71 = 88 : Even
18 + 81 = 99 : Odd
19 + 91 = 110 : Mixed
20 + 2 = 22 : Even
21 + 12 = 33 : Odd
22 + 22 = 44 : Even
23 + 32 = 55 : Odd
24 + 42 = 66 : Even
25 + 52 = 77 : Odd
26 + 62 = 88 : Even
27 + 72 = 99 : Odd
28 + 82 = 110 : Mixed
29 + 92 = 121 : Mixed
30 + 3 = 33 : Odd
31 + 13 = 44 : Even
32 + 23 = 55 : Odd
33 + 33 = 66 : Even
34 + 43 = 77 : Odd
35 + 53 = 88 : Even
36 + 63 = 99 : Odd
37 + 73 = 110 : Mixed
38 + 83 = 121 : Mixed
39 + 93 = 132 : Mixed
40 + 4 = 44 : Even
41 + 14 = 55 : Odd
42 + 24 = 66 : Even
43 + 34 = 77 : Odd
44 + 44 = 88 : Even
45 + 54 = 99 : Odd
46 + 64 = 110 : Mixed
47 + 74 = 121 : Mixed
48 + 84 = 132 : Mixed
49 + 94 = 143 : Mixed
50 + 5 = 55 : Odd
51 + 15 = 66 : Even
52 + 25 = 77 : Odd
53 + 35 = 88 : Even
54 + 45 = 99 : Odd
55 + 55 = 110 : Mixed
56 + 65 = 121 : Mixed
57 + 75 = 132 : Mixed
58 + 85 = 143 : Mixed
59 + 95 = 154 : Mixed
60 + 6 = 66 : Even
61 + 16 = 77 : Odd
62 + 26 = 88 : Even
63 + 36 = 99 : Odd
64 + 46 = 110 : Mixed
65 + 56 = 121 : Mixed
66 + 66 = 132 : Mixed
67 + 76 = 143 : Mixed
68 + 86 = 154 : Mixed
69 + 96 = 165 : Mixed
70 + 7 = 77 : Odd
71 + 17 = 88 : Even
72 + 27 = 99 : Odd
73 + 37 = 110 : Mixed
74 + 47 = 121 : Mixed
75 + 57 = 132 : Mixed
76 + 67 = 143 : Mixed
77 + 77 = 154 : Mixed
78 + 87 = 165 : Mixed
79 + 97 = 176 : Mixed
80 + 8 = 88 : Even
81 + 18 = 99 : Odd
82 + 28 = 110 : Mixed
83 + 38 = 121 : Mixed
84 + 48 = 132 : Mixed
85 + 58 = 143 : Mixed
86 + 68 = 154 : Mixed
87 + 78 = 165 : Mixed
88 + 88 = 176 : Mixed
89 + 98 = 187 : Mixed
90 + 9 = 99 : Odd
91 + 19 = 110 : Mixed
92 + 29 = 121 : Mixed
93 + 39 = 132 : Mixed
94 + 49 = 143 : Mixed
95 + 59 = 154 : Mixed
96 + 69 = 165 : Mixed
97 + 79 = 176 : Mixed
98 + 89 = 187 : Mixed
99 + 99 = 198 : Mixed
Even: 24
Odd: 26
Mixed: 50
Program ended with exit code: 0
Aucun commentaire:
Enregistrer un commentaire