试题 历届真题 幸运数【第四届】【省赛】【B组】

试题地址http://lx.lanqiao.cn/problem.page?gpid=T2927由于试题比较简单,处理好边界问题就行了


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

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();

		int[] arr = new int[n / 2 + 1];

//		直接跳步生成 0 - n 的除去 偶数的数组
		for (int i = 1; i < n / 2 + 1; i++) {
			arr[i] = 2 * (i - 1) + 1;
		}
		int luckyNum = 2;

//		维护数组和幸运数字的值就好了
		while (luckyNum < arr.length) {
			int t = 1;
			int[] temp = new int[arr.length];
			for (int i = 1; i < arr.length; i++) {
				if (i % arr[luckyNum] != 0) {
					temp[t++] = arr[i];
				}
			}
			arr = Arrays.copyOf(temp, t);
			luckyNum++;
		}

//		遍历最终数组的时候,拿  m 和 n 筛选数据
		int count = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] > m && arr[i] < n) {
				count++;
			}
		}

		System.out.println(count);

	}

}

优化版本暂时没有想出来

posted @ 2022-12-27 14:25  兑生  阅读(23)  评论(0)    收藏  举报  来源
Live2D