double 的格式化 输出

// poj 2350 import java.io.*; import java.util.*; import java.text.DecimalFormat; // add this. public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int[] a = new int[1010]; int ans, sum, i, n, Case, testCase; testCase = cin.nextInt(); for (Case = 0; Case < testCase; ++Case) { n = cin.nextInt(); for (i = sum = 0; i < n; ++i) { a[i] = cin.nextInt(); sum += a[i]; } for (i = ans = 0; i < n; ++i) if (a[i] * n > sum) ++ans; DecimalFormat df = new DecimalFormat("0.000"); // notice here System.out.println(df.format((double) (ans * 100.0) / n) + "%"); } } }

你可能感兴趣的:(double 的格式化 输出)