- 输入一个学生的成绩,如果学习成绩>=90分的同学用A表示,60-89分之间用B表示,60分以下的用C表示(10)
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println(“请输入学生成绩:”);
Int grade = scanner.nextInt();
If(grade >=90){
System.out.println(‘A’);
}else if(grade >= 60){
System.out.println(‘B’);
}else{
System.out.println(‘C’);
}
}
- 编写一个java程序在屏幕上输出1!+2!+3!+……+10!的和(10)
public class test {
public static void main(String[] args) {
int sum = 0;
int res = 1;
for(int i = 1; i<=10;i++){
res = sum*i;
sum += res;
}
System.out.println("1!+2!+3!+……+10!的和是" + sum);
}
}
int [] arr1 = new int[3];
int [] arr2 = new int[4];
for(int i = 0;i <arr1.length;i++){
arr1[i] = (int)(Math.random()*10);
}
for(int i = 0;i <arr1.length;i++){
arr1[i] = (int)(Math.random()*10);
}
int[] arr3 = new int[arr1.length + arr2.length];
for (int i = 0; i < arr1.length; i++) {
arr3[i] = arr1[i];
}
for (int i = 0; i < arr2.length; i++) {
arr3[arr3.length+i-arr2.length] = arr2[i];
}
for(int i = 0;i < arr3.length - 1;i++){
for(int j = 0;j <arr3.length - i - 1;j++){
if(arr3[j] < arr3[j+1]){
int temp = arr3[j];
arr3[j] = arr3[j+1];
arr3[j+1] = temp;
}
}
}
System.out.println("合并后的数组是:");
for(int v:arr3){
System.out.print(v+"\t");
}
- 15个猴子围成一圈选大王,依次1-7循环报数,报到7的猴子被淘汰,直到最后一只猴子称为大王,问:哪只猴子会成为大王? 编写程序实现(20)
第五只称王
- 15个猴子围成一圈选大王,依次1-7循环报数,报到7的猴子被淘汰,直到最后一只猴子称为大王,问:哪只猴子会成为大王? 编写程序实现(20)
第五只称王
int monkeyCount = 15; int eliminateCount = 7; boolean[] monkeys = new boolean[monkeyCount]; for (int i = 0; i < monkeyCount; i++) { monkeys[i] = true; } int remainingMonkeys = monkeyCount; int currentIndex = 0; while (remainingMonkeys > 1) { int count = 0; while (count < eliminateCount) { if (monkeys[currentIndex]) { count++; } currentIndex = (currentIndex + 1) % monkeyCount; } monkeys[(currentIndex + monkeyCount - 1) % monkeyCount] = false; remainingMonkeys--; } int kingIndex = 0; while (!monkeys[kingIndex]) { kingIndex++; } int kingNumber = kingIndex + 1; System.out.println("最终的大王编号是:" + kingNumber);
int monkeyCount = 15; int eliminateCount = 7; boolean[] monkeys = new boolean[monkeyCount]; for (int i = 0; i < monkeyCount; i++) { monkeys[i] = true; } int remainingMonkeys = monkeyCount; int currentIndex = 0; while (remainingMonkeys > 1) { int count = 0; while (count < eliminateCount) { if (monkeys[currentIndex]) { count++; } currentIndex = (currentIndex + 1) % monkeyCount; } monkeys[(currentIndex + monkeyCount - 1) % monkeyCount] = false; remainingMonkeys--; } int kingIndex = 0; while (!monkeys[kingIndex]) { kingIndex++; } int kingNumber = kingIndex + 1; System.out.println("最终的大王编号是:" + kingNumber);