Poj 2350 Above Average(精度控制)

来源:互联网 发布:centos开启snmp 编辑:程序博客网 时间:2024/04/30 06:59

一、Description

It is said that 90% of frosh expect to be above average in their class. You are to provide a reality check.

Input

The first line of standard input contains an integer C, the number of test cases. C data sets follow. Each data set begins with an integer, N, the number of people in the class (1 <= N <= 1000). N integers follow, separated by spaces or newlines, each giving the final grade (an integer between 0 and 100) of a student in the class.

Output

For each case you are to output a line giving the percentage of students whose grade is above average, rounded to 3 decimal places.
二、题解
        本题难度不大,只需先求出平均值,然后再依个比较,算出比平均值大的个数,再除以总人数就得所求。要注意精度控制,保留小数点三位。这里用了DecimalFormat类,

DecimalFormatNumberFormat 的一个具体子类,用于格式化十进制数字。该类设计有各种功能,使其能够解析和格式化任意语言环境中的数,包括对西方语言、阿拉伯语和印度语数字的支持。它还支持不同类型的数,包括整数 (123)、定点数 (123.4)、科学记数法表示的数 (1.23E4)、百分数 (12%) 和金额 ($123)。所有这些内容都可以本地化。

System.out.println(new java.text.DecimalFormat("0.000").format(per)+"%");
三、java代码
import java.util.Scanner;  public class Main {          public static void main(String[] args) {           Scanner sc=new Scanner(System.in);        int[] a=new int[1000];        int m,i,sum,num;        double average,per;        int n=sc.nextInt();        while(n--!=0){        m=sc.nextInt();        sum=0;        num=0;        for(i=0;i<m;i++){        a[i]=sc.nextInt();        sum+=a[i];        }        average=1.0*sum / m;        for(i=0;i<m;i++){        if(a[i]>average)        num++;        }        per=(1.0*num)/m *100;        System.out.println(new java.text.DecimalFormat("0.000").format(per)+"%");        }    }  }