初级题:从考试成绩中划出及格线
10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:
(1) 及格线是10的倍数;
(2) 保证至少有60%的学生及格;
(3) 如果所有的学生都高于60分,则及格线为60分
输入:输入10个整数,取值0~100
输出:输出及格线,10的倍数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[] a = new int[10];
int put = 0;
for(int i=0;i<10;i++) {
if(s.hasNext())
a[i] = s.nextInt();
}
if(Judge(a)) {
put = 60;
}
else {
sort(a);
switch(a[(int) (a.length*0.4)]/10) {
case 10: put = 60;break;
case 9: put = 60;break;
case 8: put = 60;break;
case 7: put = 60;break;
case 6: put = 60;break;
case 5: put = 50;break;
case 4: put = 40;break;
case 3: put = 30;break;
case 2: put = 20;break;
case 1: put = 10;break;
case 0: put = 0;break;
default: break;
}
}
System.out.print(put);
}
private static void sort(int[] a) {
int temp = 0;
for(int i=0;i<a.length-1;i++) {
for(int j=0;j<a.length-i-1;j++) {
if(a[j]>a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
private static boolean Judge(int[] a) {
for(int i=0;i<a.length;i++) {
if(a[i]<60)
return false;
}
return true;
}
}
中级题:亮着电灯的盏数
一条长廊里依次装有n(1 ≤ n ≤ 65535)盏电灯,从头到尾编号1、2、3、…n-1、n。每盏电灯由一个拉线开关控制。开始,电灯全部关着。
有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下;接着第二个学生把号码凡是2的倍数的电灯的开关拉一下;接着第三个学生把号码凡是3的倍数的电灯的开关拉一下;如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。
注:电灯数和学生数一致。
输入:电灯的数量
输出:亮着的电灯数量
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
int sum = 0,count,number;
Scanner s = new Scanner(System.in);
number = s.nextInt();
for(int i=1;i<=number;i++) {
count = 0;
for(int j=1;j<=i;j++) {
if(i%j==0) {
count++;
}
}
if(count%2==1) {
sum++;
}
}
System.out.print(sum);
}
}
高级题:地铁换乘
已知2条地铁线路,其中A为环线,B为东西向线路,线路都是双向的。经过的站点名分别如下,两条线交叉的换乘点用T1、T2表示。编写程序,任意输入两个站点名称,输出乘坐地铁最少需要经过的车站数量(含输入的起点和终点,换乘站点只计算一次)。
地铁线A(环线)经过车站:A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18
地铁线B(直线)经过车站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15
输入:输入两个不同的站名
输出:输出最少经过的站数,含输入的起点和终点,换乘站点只计算一次
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
String[] a = {"A1","A2","A3","A4","A5","A6","A7","A8","A9","T1","A10",
"A11","A12","A13","T2","A14","A15","A16","A17","A18"};
String[] b = {"B1","B2","B3","B4","B5","T1","B6","B7","B8","B9","B10",
"T2","B11","B12","B13","B14","B15"};
Scanner s = new Scanner(System.in);
String input1 = s.next();
String input2 = s.next();
int sum = Calculate(a,b,input1,input2);
System.out.print(sum);
}
private static int Calculate(String[] a, String[] b, String input1,
String input2) {
boolean flag1=false,flag2=false,flag3=false,flag4=false;
int index1=0,index2=0;
int sum = 0;
int length1[]=new int[2],length2[] = new int[2];
for(int i=0;i<a.length;i++) {
if(input1.equalsIgnoreCase(a[i])) {
index1 = i;
flag1 = true;
}
if(input2.equalsIgnoreCase(a[i])) {
index2 = i;
flag2 = true;
}
}
for(int j=0;j<b.length;j++) {
if(input1.equalsIgnoreCase(b[j])) {
index1 = j;
flag3 = true;
}
if(input2.equalsIgnoreCase(b[j])) {
index2 = j;
flag4 = true;
}
}
if(flag1) {
length1[0] = Math.abs(index1-9)+1;
length1[1] = Math.abs(index1-13)+1;
}
if(flag2) {
length2[0] = Math.abs(index2-9)+1;
length2[1] = Math.abs(index2-13)+1;
}
if(flag3) {
length1[0] = Math.abs(index1-5)+1;
length1[1] = Math.abs(index1-11)+1;
}
if(flag4) {
length2[0] = Math.abs(index2-5)+1;
length2[1] = Math.abs(index2-11)+1;
}
if((length1[0]+length2[0]-1)<(length1[1]+length2[1]-1)) {
sum = length1[0]+length2[0]-1;
}
else
sum = length1[1]+length2[1]-1;
return sum;
}
}