15个猴子围成一圈选大王,依次1-7循环报数,报到7的猴子被淘汰,直到最后一只猴子称为大王,问:哪只猴子会成为大王?
package 练习; public class MonkeyKing { public static void main(String[] args) { boolean[] b=new boolean[15]; for(int i=0;i<b.length;i++){ b[i]=true; int num=0; int monkeyLeft=15; int index=0; while(monkeyLeft>1) { if(b[index]){ num++; if(num==7){ b[index]=false; monkeyLeft--; num=0; } } index++; if(index==15){ index=0; } } for(int i=0;i<b.length;i++){ if(b[i]){ System.out.println(i+1); } } } }
已知:斐波那契数列的前几个数分别为0,1,1,2,3,5…从第三项开始,每一项都等于前两项的和.请接收用户输入的整数n,求出此数列的前n项.
package 练习; import java.util.Scanner; public class Faibonacci { public static void main(String[] args) { System.out.println("请输入您要测试的数:"); int n = new Scanner(System.in).nextInt(); if(n<1){ System.out.println("输入数据有误!!!"); } if(n==1){ System.out.println(0); } if(n==2){ System.out.println(0+"\t"+1); } if(n==3){ System.out.println(0+"\t"+1+"\t"+1); } if(n>3){ System.out.print(0+"\t"+1+"\t"+1+"\t"); } int f1=1; int f2=1; int next=0; for(int i=4;i<=n;i++){ next=f1+f2; f1=f2; f2=next; System.out.print(next+"\t"); } } }
标签:index,15,int,08,System,num,2023,public From: https://www.cnblogs.com/zhenaifen/p/17631800.html