結果

問題 No.6 使いものにならないハッシュ
ユーザー DAZY
提出日時 2017-05-29 01:38:10
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,463 ms / 5,000 ms
コード長 1,825 bytes
コンパイル時間 3,638 ms
コンパイル使用メモリ 77,760 KB
実行使用メモリ 42,196 KB
最終ジャッジ日時 2024-09-16 16:40:23
合計ジャッジ時間 27,372 ms
ジャッジサーバーID
(参考情報)
judge3 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class No6 {
    public static boolean checkPrime (int n) {
        boolean b = true;
        if (n==2 || n==3 || n==5) ;
        else if ((n & 1)==0 || n==1) b = false;
        else {
            for (int i = 3; i < n/2; i+=2) {
                if (n % i==0) {
                    b = false;
                    break;
                }
            }
        }
        return b;
    }
    public static int sumEachbit (int n) {
        int sum;
        while (n > 9) {
            sum = 0;
            for (; n > 0;) {
                sum += n%10;
                n /= 10;
            }
            n = sum;
        }
        return n;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int K = scan.nextInt();
        int N = scan.nextInt();
        int[] S = new int[N+1];
        int check[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int ans = 0, max = 0, tmp1 = 0, tmp2 = 0, tmp3 = 0;
        for (int i = K; i <= N; i++) {
            if (checkPrime(i)) {
                S[i] = i;
                if (S[i] > 9) S[i] = sumEachbit(i);
            } else S[i] = -1;
        }
        for (int i = K; i <= N; i++) {
            if (S[i] == -1);
            else {
                if (tmp1 == 1) tmp3 = i;
                if (check[S[i]] == 0) {
                    if (tmp1 == 0) tmp2 = i;
                    tmp1++;
                    check[S[i]] = 1;
                } else {
                    if (tmp1 >= max) {
                        max = tmp1;
                        ans = tmp2;
                    }
                    tmp1 = 0;
                    Arrays.fill(check, 0);
                    i = tmp3 - 1;
                }
            }
        }
        if (tmp1 >= max) ans = tmp2;
        System.out.println(ans);
    }
}
0