码迷,mamicode.com
首页 > 其他好文 > 详细

[HDU 1202] The calculation of GPA

时间:2019-03-17 11:03:05      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:学分   uri   问题:   out   情况下   sys   rgs   初始   href   

题目链接: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;
    }
}

[HDU 1202] The calculation of GPA

标签:学分   uri   问题:   out   情况下   sys   rgs   初始   href   

原文地址:https://www.cnblogs.com/youpeng/p/10545723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!