第14届蓝桥杯 三月刷题第2天

浅夜呀 2023-03-05 23:07:10

奇数倍数

public class Main {
    public static void main(String[] args) {
        for (int i = 2019; ; i += 2019) {
            if (check(i)) {
                System.out.println(i);
                break;
            }
        }
    }

     static boolean check(int i) {
        while (i > 0) {
            if (i%2 == 0) return false;
            i /= 10;
        }
        return true;
    }
}

求值

public class Main {

    public static void main(String[] args) {
        for (int i = 2; ; i++) {
            int ans = 0;
            for (int j = 1; j < i / j; j++) {
                if (i % j == 0) ans++;
            }
            if (ans == 50) {
                System.out.println(i);
                return;
            }
        }
    }
}

求和

import java.util.Scanner;

public class Main {
    static int N = 200010;
    static long[] a = new long[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }
        long ans = 0;//保存结果
        long sum = 0;//保存前i-1项的和
        for (int i = 1; i <= n; i++) {
            ans += a[i] * sum;
            sum += a[i];
        }
        System.out.println(ans);
    }
}

数位排序

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        //要自定义排序的话 要用 Integer[]
        Integer[] a = new Integer[n];
        for (int i = 0; i < n; i++) a[i] = i + 1;
        //自定义排序
        Arrays.sort(a, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                int a = check(o1);
                int b = check(o2);
                if (a != b) return a - b;
                else return o1 - o2;
            }
        });
        System.out.println(a[m - 1]);
    }

    //求数位和
    private static int check(Integer x) {
        int res = 0;
        while (x != 0) {
            res += x % 10;
            x /= 10;
        }
        return res;
    }
}

 

 

...全文
8 回复 打赏 收藏 举报
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
发帖
高校算法学习社区

4.3w+

社区成员

和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
帖子事件
创建了帖子
2023-03-05 23:07
社区公告

 刷题!