35選7
package com.neuedu.chapter3_0304;
import java.util.Random;
public class Practice5_Work {
public static void main(String[] args) {
// 模擬35選7 生成7個(gè)不同的1-35之間的數(shù)(只用一個(gè)循環(huán))
Random ran= new Random();
// int num =ran.nextInt(35)+1;//[1跷睦,36)
int arr[]=new int[7];
boolean b[]=new boolean[35];
for(int i = 0;i<7;i++) {
int num =ran.nextInt(35)+1;
if(b[num-1]) {
i--;
}else {
b[num-1]=true;
System.out.print(num+" ");
}
}
}
}
斯巴達(dá)500勇士
package com.neuedu.chapter3_0304;
public class Practice6_Work {
public static void main(String[] args) {
// 500勇士 12x 12x 12x 12x 12x 報(bào)到最后一個(gè)人時(shí)筷弦,再?gòu)拈_(kāi)始或者的人接著報(bào)數(shù)
//如此往復(fù),最后就剩一個(gè)人抑诸,求他所在位置
boolean b[] =new boolean[500];//500個(gè)false烂琴,false代表活著,true已經(jīng)gg
int rest = 500;// 生還人數(shù)
int count = 1;// 報(bào)數(shù)
int i= 0;
while(rest > 1) {
// 先判斷生死
if(!b[i]) {
// 判斷報(bào)的數(shù)字是不是3
if(count == 3) {
// 干掉
b[i] = true;
// 人數(shù)自減
rest--;
// 報(bào)的數(shù)字清0
count = 0;
}
// 報(bào)數(shù)自增
count++;
}
i++;
if(i == 500) {
i=0;
}
}
for(int j = 0; j <b.length;j++) {
if(b[j] == false) {
System.out.println(j+1);
}
}
}
}