用java编程实现从0到100组成一个环,从第一个开始数,每次到第7个就输出,最后一个剩余的是几?
用java编程实现从0到100组成一个环,从第一个开始数,每次到第7个就输出,最后一个剩余的是几?
程序代码: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编辑过]








