程序源码:
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner (System.in);
int n = in.nextInt();
int []num = new int[n];
for (int i =0; i< n; i++) num[i] = in.nextInt();
in.close();
int max,min;
double avc;
if (num[0]>num[1]) {
max = num[0];
min = num[n-1];
}else {
max = num[n-1];
min = num[0];
}
if (n%2 == 0) avc = ((num[n/2-1]+num[n/2])/2.0);
else avc = num[n/2];
if (((num[n/2-1]+num[n/2]) %2 == 0) || (n+1)%2 == 0)
System.out.print(max+" "+(int)(avc)+" "+min);
else {
System.out.print(max+" "+String.format("%.1f", avc)+" "+min);
}
}
}
备注:
当时做题目的时候用了好多的方法,发现都是不对的,永远都是在eclipse上编译通过,但是在CCF提交以后只能得30分,显示错误。一直弄,弄了一个多小时接近2个小时,终于明白自己的问题出在哪里了。
因为一开始我一直用C写CCF。然后现在突然转型用java,在C语言中是不允许有:
int n = 5;
int num[n];
这种写法在C/C++里面是不行的,除非加const,但是在java里面允许变量赋值给数组的存在。在此给自己留下记忆性的一笔。
重点:
java保留小数位数方法(方法有很多,这里写两个个人比较喜欢的方法):
import java.text.DecimalFormat;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(f));
或者是:
System.out.println(String.format("%.2f", num));
前面两种都是最简单粗暴的方法,或者也可以使用:
import java.math.BigDecimal;
BigDecimal bg = new BigDecimal(f);
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);