用java编程实现从0到100组成一个环,从第一个开始数,每次到第7个就输出,最后一个剩余的是几?
用java编程实现从0到100组成一个环,从第一个开始数,每次到第7个就输出,最后一个剩余的是几?
2019-03-16 13:26
程序代码:public class Test{
public static void main(String... args){
boolean[] flags = new boolean[100]; //数字是否被移除
int cursor = 0; //游标
int i ; //小循环游标 i = cursor%100
int count = 0; //已移除个数
int j = 0; //数字有效次数。未被移除记有效次数,否则不计数。
while(count!=100){
i = cursor%100;
if(!flags[i]) //未被移除,数字有效
j++;
if(j%7==0){ //有效次数,逢7移除,移除个数+1。
flags[i]=true;
count++;
System.out.println(count +" "+(i+1)+" "+(cursor+1));// 移除顺序、数字、循环次数。
}
cursor++;
}
}
}
程序代码:public static int test(int start, int end, int step) {
int count = end - start + 1;
boolean[] flags = new boolean[count];
int cursor = 0;
int i;
int del_count = 0;
int j = 0;
int result = 0;
while (del_count != count) {
i = cursor % count;
if (!flags[i]) {
j++;
if (j % step == 0) {
flags[i] = true;
del_count++;
result = i + start;
// System.out.println(j + " " + del_count + " " + (i + start) + " " + (cursor));
}
}
cursor++;
}
return result;
}[此贴子已经被作者于2019-3-26 10:13编辑过]

2019-03-19 22:44
程序代码:public class Test {
public static void main(String[] args) {
int count = 101;
int index = 0;
List<Integer> list = new ArrayList<>();
while(count - list.size() > 1) {
for(int i = 0; i < 7; i++) {
index++;
if(index >= count) {
index = index % count;
}
if(list.contains(index)){
i--;
}
}
list.add(index);
}
System.out.println(index);
}
}
[此贴子已经被作者于2019-3-22 10:01编辑过]

2019-03-21 20:41