[HDU 1202] The calculation of GPA

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1202

题目不难,注意一些细节问题:

  • 浮点数初始化或者进行比较的时候默认加上个 .0
  • 认真分析题目中的各项数据应该用什么类型的
  • 在测试用例不确定的情况下,还是用while来无限次输入吧
/*
 * 用while循环
 * 学分和成绩数据是浮点数
 */

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            double x,y;
            double suma = 0.0,sumb = 0.0;
            while (n-- > 0) {
                x = scanner.nextDouble();
                y = scanner.nextDouble();
                if (y == -1.0)
                    continue;
                suma += x;
                sumb = sumb + x * dian(y);
            }
            if (suma == 0.0) {
                System.out.println(-1);
            } else {
                double ans = sumb / suma;
                System.out.printf("%.2f", ans);
                System.out.println();
            }
        }
    }

    public static int dian(double y) {
        if (y >= 90.0)
            return 4;
        else if (y >= 80.0)
            return 3;
        else if (y >= 70.0)
            return 2;
        else if (y >= 60.0)
            return 1;
        else
            return 0;
    }
}

猜你喜欢

转载自www.cnblogs.com/youpeng/p/10545723.html
GPA