zoukankan      html  css  js  c++  java
  • 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)+"%");
            }
        }  
    }   
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    linux shell 的切换
    linux:TeamViewer安装使用详解
    虚拟机与主机互ping
    每R一点:层次聚类分析实例实战-dist、hclust、heatmap等(转)
    每R一点:各种画地图,全是知识点,90%人不知道!(转)
    R语言学习路线和常用数据挖掘包(转)
    SAS PROC MCMC example in R: Logistic Regression Random-Effects Model(转)
    SparkR安装部署及数据分析实例
    R语言的导数计算(转)
    awk之随机函数rand()和srand() (转)
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734135.html
Copyright © 2011-2022 走看看