Poj 2350 Above Average(精度控制)

一、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)+"%");
        }
    }  
}   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值