-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_0970_powerfulIntegers.cc
65 lines (57 loc) · 1.06 KB
/
Problem_0970_powerfulIntegers.cc
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
#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 枚举
vector<int> powerfulIntegers(int x, int y, int bound)
{
unordered_set<int> cnt;
int a = 1;
for (int i = 0; i < 21; i++)
{
int b = 1;
for (int j = 0; j < 21; j++)
{
int value = a + b;
if (value <= bound)
{
cnt.emplace(value);
}
else
{
break;
}
b *= y;
}
if (a > bound)
{
break;
}
a *= x;
}
return vector<int>(cnt.begin(), cnt.end());
}
};
void testPowerfulInteger()
{
Solution s;
vector<int> o1 = {2, 3, 4, 5, 7, 9, 10};
vector<int> o2 = {2, 4, 6, 8, 10, 14};
vector<int> a = s.powerfulIntegers(2, 3, 10);
std::sort(a.begin(), a.end());
vector<int> b = s.powerfulIntegers(3, 5, 15);
std::sort(b.begin(), b.end());
EXPECT_TRUE(o1 == a);
EXPECT_TRUE(o2 == b);
EXPECT_SUMMARY;
}
int main()
{
testPowerfulInteger();
return 0;
}