結果

問題 No.6 使いものにならないハッシュ
ユーザー drymousedrymouse
提出日時 2024-02-14 17:02:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,012 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 92,808 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-14 17:02:32
合計ジャッジ時間 2,796 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:88:13: warning: 'maxfirst' may be used uninitialized [-Wmaybe-uninitialized]
   88 |     cout << maxfirst << endl;
      |             ^~~~~~~~
main.cpp:54:9: note: 'maxfirst' was declared here
   54 |     int maxfirst;
      |         ^~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;

bool is_prime(int a) {
    if (a < 4) {
        if (a == 1) {return false;}
        else {return true;}
    } else if (a % 2 == 0) {
        return false;
    }
    for (int i = 3; i < sqrt(a) + 2; i+=2) {
        if (a % i == 0) {return false;}
    }
    return true;
}

int sum_digit(int a) {
    int x = a;
    int sum = 0;
    for (int i = 0; i < 6; i++) {
        sum += x % 10;
        x /= 10;
    }
    return sum;
}

int frank_hash(int a) {
    int sum = a;

    for (;sum > 9;) {
        sum = sum_digit(sum);
    }
    return sum;
}

void output(vector<int> a) {
    int l = a.size();
    cout << "[";
    for (int i = 0; i < l; i++) {
        cout << a[i] << ", ";
    }
    cout << "]" << endl;
}

int main(void) {
    int K, N;
    cin >> K >> N;
    cout << sum_digit(K) << endl;
    cout << frank_hash(K) << endl;

    int maxfirst;
    int maxlen = 0;
    int curlen = 0;
    vector<int> ps, hs;

    for (int i = K; i < N + 1; i++) {
        if (!is_prime(i)) {continue;}
        int h = frank_hash(i);
        bool is_exest = false;
        for (int j = 0; j < hs.size(); j++) {
            if (hs[j] == h) {
                if (curlen >= maxlen) {
                    maxlen = curlen;
                    maxfirst = ps[0];
                }
                curlen -= j + 1;
                ps.erase(ps.begin(), ps.begin() + j + 1);
                hs.erase(hs.begin(), hs.begin() + j + 1);
                is_exest = true;
                break;
            }
        }
        ps.push_back(i);
        hs.push_back(h);
        curlen++;
        //cout << i << ", " << h << ", " << curlen << ", " << maxlen << ", " << maxfirst << endl;
        //output(ps);
        //output(hs);
    }
    if (curlen >= maxlen) {
        maxlen = curlen;
        maxfirst = ps[0];
    }
    //cout << "max first: " << maxfirst << ", max len: " << maxlen << endl;
    cout << maxfirst << endl;

    return 0;
}
0