蓝桥杯 幸运数 JAVA

思路:输入n,m两数(n<m),先将1~m的数字添加到List中,然后从幸运数2开始,将2到List.size()大小的数字中的下标能被幸运数2整取的数字添加到

一个名为remove的List中,然后将remove中的数字作为list中的下标将list中对应的数字删除。

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.Scanner;
 4 
 5 public class 幸运数 {
 6     public static void main(String[] args) {
 7         Scanner sc = new Scanner(System.in);
 8         int n = sc.nextInt();
 9         int m = sc.nextInt();
10         
11         List<Integer> list = new ArrayList<>();
12         for(int i=1;i<=m;i++){
13             list.add(new Integer(i));
14         }
15         
16         int luck = 2; //幸运数
17         int k = 1;  //下标
18         List<Integer> remove = new ArrayList<>();  //等待被移除的数字
19         while(luck<=list.size()){
20             int cnt = 0;//代表list中目前删除了几个数字
21             for(int i=luck;i<=list.size();i++){  
22                 if(i%luck==0){
23                     remove.add(i);
24                 }
25             }
26             
27             
28             for(int j=0;j<remove.size();j++){//将下标为remove中的数字移除
29                 list.remove(remove.get(j)-(cnt++)-1);
30             }
31             remove.clear();
32 
33             luck = list.get(k++);
34         }
35         int count = 0;
36         for(int i=0;i<list.size();i++){
37             if(list.get(i)>n&&list.get(i)<m){
38                 count++;
39             }
40         }
41         System.out.println(count);
42     }
43 }
原文地址:https://www.cnblogs.com/blzm742624643/p/10362433.html