PAT 1004. 成绩排名 (20) JAVA

来源:互联网 发布:java必看书籍知乎 编辑:程序博客网 时间:2024/06/06 17:23

1004. 成绩排名 (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:每个测试输入包含1个测试用例,格式为

  第1行:正整数n  第2行:第1个学生的姓名 学号 成绩  第3行:第2个学生的姓名 学号 成绩  ... ... ...  第n+1行:第n个学生的姓名 学号 成绩
其中姓名和学号均为不超过10个字符的字符串,成绩为0到100之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

输出格式:对每个测试用例输出2行,第1行是成绩最高学生的姓名和学号,第2行是成绩最低学生的姓名和学号,字符串间有1空格。

首先获取所有行的字符串,再迭代出最大值和最小值。

import java.util.Scanner;/*读入所有的行然后迭代用两个交换变量存储max 和 min的数组注意:三位数、二位数、一位数的不同处理。 */public class no1004 {    public static void main(String args[]){        Scanner scanner =new Scanner(System.in);        int circlesum=new Integer(scanner.nextLine());        String[] input =new String[circlesum];        for (int q=0;q<circlesum;q++){            input[q]=scanner.nextLine();        }        String temp_max ="",temp_min="";        int temp_max_values=-1,temp_min_values=101;        boolean is_thress=false,is_one=false;        int temp=0;        for(String ss :input){            try {//三位数处理                temp =new Integer(ss.substring(ss.length()-3,ss.length()));                is_thress=true;            }catch (Exception e){//二位数处理                try {                    temp= new Integer(ss.substring(ss.length() - 2, ss.length()));                }catch (Exception ee){//一位数处理                    temp =new Integer(ss.substring(ss.length()-1,ss.length()));                    is_one=true;                }            }            if(temp>temp_max_values){                if(is_thress){                    temp_max=ss.substring(0,ss.length()-4);                }else if(is_one) {                    temp_max=ss.substring(0,ss.length()-2);                }else {                        temp_max=ss.substring(0,ss.length()-3);                    }                temp_max_values=temp;            }            if(temp<temp_min_values){                if(is_thress){                    temp_min=ss.substring(0,ss.length()-4);                }else if(is_one) {                    temp_min=ss.substring(0,ss.length()-2);                }else {                        temp_min=ss.substring(0,ss.length()-3);                    }                temp_min_values=temp;            }            is_thress=false;            is_one=false;        }        System.out.println(temp_max);        System.out.println(temp_min);    }}

原创粉丝点击