算法竞赛入门经典java版程序ch4 UVa12412

package ch4.uva12412;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

class Student implements Comparable<Student> {
	String sid;
	String cid;
	String name;
	int chinese;
	int math;
	int english;
	int prog;
	//
	int total;
	float avg;
	int rank;

	public String toString() {
		return rank+" "+sid + " " + cid + " " + name + " " + chinese + " " + math + " " + english + " " + prog + " " + total + " " + String.format("%.2f", avg);
	}

	@Override
	public int compareTo(Student s) {
		if (this.total > s.total)
			return -1;
		else if (this.total < s.total)
			return 1;
		else
			return 0;
	}
}

class Main {
	public static Scanner cin;
	public static int choice;

	public static ArrayList<Student> stuList = new ArrayList<Student>();
	public static ArrayList<Student> stuList2 = new ArrayList<Student>();

	public static void main(String[] args) throws Exception{
		showMainUI();
		cin = new Scanner(System.in);
		//cin=new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("12412.in"))));
		while ((choice = cin.nextInt()) >= 0) {
			if (choice == 1) {
				menuAdd();
			} else if (choice == 2) {
				menuRemove();
			} else if (choice == 3) {
				menuQuery();
			} else if (choice == 4) {
				menuShowRanking();
			} else if (choice == 5) {
				menuShowStatics();
			} else {
				menuExit();
			}
		}
	}

	// copy stuList到新数组,并计算rank
	public static void copyForRank() {
		stuList2.clear();
		for (int i = 0; i < stuList.size(); i++) {
			stuList2.add(stuList.get(i));
		}
		Collections.sort(stuList2);// 升序排序
		int rank = 0;
		int k=1;//向后滑几个rank计数
		int lastTotal = -1;//防止0分出现
		for (int i = 0; i < stuList2.size(); i++) {
			if (stuList2.get(i).total != lastTotal) {
				rank=rank+k;
				k=1;
			}else{
				k++;
			}
			stuList2.get(i).rank = rank;
			lastTotal = stuList2.get(i).total;
		}
	}

	// 根据sid找,如果没找到返回null
	public static Student findStudentBySid(String sid) {
		for (int i = 0; i < stuList.size(); i++) {
			if (sid.equals(stuList.get(i).sid)) {
				return stuList.get(i);
			}
		}
		return null;
	}

	// 根据sid remove
	public static int removeStudentBySid(String sid) {
		int count = 0;
		for (int i = 0; i < stuList.size(); i++) {
			if (sid.equals(stuList.get(i).sid)) {
				stuList.remove(i);
				i--;
				count++;
			}
		}
		return count;
	}

	// 根据名称查找,如果没找到.list.size()=0
	public static ArrayList<Student> findStudentByName(String name) {
		ArrayList<Student> list = new ArrayList<Student>();
		for (int i = 0; i < stuList.size(); i++) {
			if (name.equals(stuList.get(i).name)) {
				list.add(stuList.get(i));
			}
		}
		return list;
	}

	// 根据名称remove
	public static int removeStudentByName(String name) {
		int count = 0;
		for (int i = 0; i < stuList.size(); i++) {
			if (name.equals(stuList.get(i).name)) {
				stuList.remove(i);
				i--;//重要,以防止后面的元素移上来,下一次没有操作
				count++;
			}
		}
		return count;
	}

	// 根据班级号显示统计信息
	public static void showStatisticsBySid(String cid) {
		ArrayList<Student> list = new ArrayList<Student>();
		for (int i = 0; i < stuList.size(); i++) {
			if (cid.equals(stuList.get(i).cid)) {
				list.add(stuList.get(i));
			}
		}
		printStatistics(list);
	}

	// 找印指定范围的学生统计信息
	public static void printStatistics(ArrayList<Student> list) {
		// caculate
		int sumChn = 0, cntChn = 0;
		int sumMath = 0, cntMath = 0;
		int sumEng = 0, cntEng = 0;
		int sumProg = 0, cntProg = 0;
		int cntTotal = 0;
		int[] cntPass = new int[5];
		for (int i = 0; i < list.size(); i++) {
			cntTotal = 0;
			sumChn += list.get(i).chinese;
			sumMath += list.get(i).math;
			sumEng += list.get(i).english;
			sumProg += list.get(i).prog;
			if (list.get(i).chinese >= 60) {
				cntChn++;
				cntTotal++;
			}
			if (list.get(i).math >= 60) {
				cntMath++;
				cntTotal++;
			}
			if (list.get(i).english >= 60) {
				cntEng++;
				cntTotal++;
			}
			if (list.get(i).prog >= 60) {
				cntProg++;
				cntTotal++;
			}
			// 计算通过门数
			if (cntTotal >= 4) {
				cntPass[0]++;
			}
			if (cntTotal >= 3) {
				cntPass[1]++;
			}
			if (cntTotal >= 2) {
				cntPass[2]++;
			}
			if (cntTotal >= 1) {
				cntPass[3]++;
			}
			if (cntTotal == 0) {
				cntPass[4]++;
			}
		}
		System.out.println("Chinese");
		System.out.printf("Average Score: %.2f\n", 1.0 * sumChn / list.size());
		System.out.println("Number of passed students: " + cntChn);
		System.out.println("Number of failed students: " + (list.size() - cntChn));
		System.out.println();
		System.out.println("Mathematics");
		System.out.printf("Average Score: %.2f\n", 1.0 * sumMath / list.size());
		System.out.println("Number of passed students: " + cntMath);
		System.out.println("Number of failed students: " + (list.size() - cntMath));
		System.out.println();
		System.out.println("English");
		System.out.printf("Average Score: %.2f\n", 1.0 * sumEng / list.size());
		System.out.println("Number of passed students: " + cntEng);
		System.out.println("Number of failed students: " + (list.size() - cntEng));
		System.out.println();
		System.out.println("Programming");
		System.out.printf("Average Score: %.2f\n", 1.0 * sumProg / list.size());
		System.out.println("Number of passed students: " + cntProg);
		System.out.println("Number of failed students: " + (list.size() - cntProg));
		System.out.println();
		System.out.println("Overall:");
		System.out.println("Number of students who passed all subjects: " + cntPass[0]);
		System.out.println("Number of students who passed 3 or more subjects: " + cntPass[1]);
		System.out.println("Number of students who passed 2 or more subjects: " + cntPass[2]);
		System.out.println("Number of students who passed 1 or more subjects: " + cntPass[3]);
		System.out.println("Number of students who failed all subjects: " + cntPass[4]);
	}

	public static void showMainUI() {
		System.out.println("Welcome to Student Performance Management System (SPMS).");
		System.out.println();
		System.out.println("1 - Add");
		System.out.println("2 - Remove");
		System.out.println("3 - Query");
		System.out.println("4 - Show ranking");
		System.out.println("5 - Show Statistics");
		System.out.println("0 - Exit");
		System.out.println();
	}

	public static void showAll() {
		System.out.println("*********");
		for (int i = 0; i < stuList.size(); i++) {
			System.out.println(stuList.get(i));
		}
	}

	public static void menuAdd() {
		cin.nextLine();// 吃回车
		System.out.println("Please enter the SID, CID, name and four scores. Enter 0 to finish.");
		String s;
		while (!(s = cin.nextLine()).equals("0")) {
			String[] arr = s.split(" ");
			Student stu = new Student();
			stu.sid = arr[0];
			stu.cid = arr[1];
			stu.name = arr[2];
			stu.chinese = Integer.valueOf(arr[3]);
			stu.math = Integer.valueOf(arr[4]);
			stu.english = Integer.valueOf(arr[5]);
			stu.prog = Integer.valueOf(arr[6]);
			stu.total = stu.chinese + stu.math + stu.prog + stu.english;
			stu.avg = stu.total / 4.0F;
			// 有点慢?每加一个都要循环去找一遍
			if (findStudentBySid(stu.sid) != null) {
				System.out.println("Duplicated SID.");
			} else {
				stuList.add(stu);
			}
			System.out.println("Please enter the SID, CID, name and four scores. Enter 0 to finish.");
		}
		
//		System.out.println("before rank....add");		
//		showAll();
		// 计算rank
		copyForRank();
		
//		System.out.println("after ... rank....add");
//		showAll();		
		showMainUI();
	}

	private static void menuExit() {
			System.exit(0);
	}

	private static void menuShowStatics() {
		cin.nextLine();// 吃回车 }
		System.out.println("Please enter class ID, 0 for the whole statistics.");
		String s = cin.nextLine();
		if (s.equals("0")) {// all
			printStatistics(stuList);
		} else {// cid
			showStatisticsBySid(s);
		}
		System.out.println();
		showMainUI();
	}

	private static void menuShowRanking() {
		System.out.println("Showing the ranklist hurts students' self-esteem. Don't do that.");
		//
		showMainUI();
	}

	private static void menuQuery() {
		cin.nextLine();// 吃回车 }
		System.out.println("Please enter SID or name. Enter 0 to finish.");
		String s;
		// 先准备好条件,在查询,更快
		while (!(s = cin.nextLine()).equals("0")) {
			if (s.charAt(0) >= '0' && s.charAt(0) <= '9') {// sid
				Student stu = findStudentBySid(s);
				if (stu != null) {
					System.out.println(stu);
				}
			} else {// name
				ArrayList list = findStudentByName(s);
				// output,如果没有不输出
				for (int i = 0; i < list.size(); i++) {
					System.out.println(list.get(i));
				}
			}
			System.out.println("Please enter SID or name. Enter 0 to finish.");
		}
		//
		showMainUI();
	}

	private static void menuRemove() {
		cin.nextLine();// 吃回车
		System.out.println("Please enter SID or name. Enter 0 to finish.");
		String s;
		// 先准备好条件,在查询,更快
		while (!(s = cin.nextLine()).equals("0")) {
			int xx = 0;
			if (s.charAt(0) >= '0' && s.charAt(0) <= '9') {// sid
				xx = removeStudentBySid(s);
				System.out.println(xx + " student(s) removed.");
			} else {// name
				xx = removeStudentByName(s);
				System.out.println(xx + " student(s) removed.");
			}
			System.out.println("Please enter SID or name. Enter 0 to finish.");
		}
		
		//System.out.println("before rank....remove");
		//showAll();
//		
		copyForRank();// 重新算rank
		showMainUI();
		
		//System.out.println("after rank....remove");
		//showAll();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值