Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Archives
Today
Total
관리 메뉴

PS 부수기

Project Euler #26 Reciprocal cycles 본문

Project Euler

Project Euler #26 Reciprocal cycles

jyheo98 2020. 8. 8. 10:36
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
int solve(int n) {
    set<int> p;
    p.insert(1);
    int num = 1;
    int numnum = -1;
    int cnt = 0;
    bool startCycle = false;
    while (1) {
        num *= 10;
        if (num % n == 0return 0;
        if (num % n == numnum) return cnt;
        if (p.count(num % n) && startCycle == false) {
            startCycle = true;
            numnum = num % n;
        }
        num = num % n;
        if (startCycle) cnt++;
        p.insert(num % n);
    }
}
 
int main() {
    IOS;
    int ans = -1, ansIndex;
    for (int i = 2; i <= 1000; i++) {
        if (solve(i) > ans) {
            ansIndex = i;
            ans = solve(i);
        }
    }
    cout << ansIndex;
}
cs

여기부턴 외국사이트에서 풀것이다!

나머지가 2번 나타났을 때부터 세기 시작해서 3번째 나타날때까지 cnt를 셌다. 너무 야매인가..

Comments