Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Histogram {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0;
- for (int i = 1; i <= n; i++) {
- int number = Integer.parseInt(scanner.nextLine());
- if (number < 200) {
- p1 += 1;
- } else if (number < 400) {
- p2 += 1;
- } else if (number < 600) {
- p3 += 1;
- } else if (number < 800) {
- p4 += 1;
- } else {
- p5 += 1;
- }
- }
- System.out.printf("%.2f%%\n", (double)p1 / n * 100);
- System.out.printf("%.2f%%\n", (double)p2 / n * 100);
- System.out.printf("%.2f%%\n", (double)p3 / n * 100);
- System.out.printf("%.2f%%\n", (double)p4 / n * 100);
- System.out.printf("%.2f%%\n", (double)p5 / n * 100);
- }
- }
- ИЛИ:
- import java.util.Scanner;
- public class Histogram {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0;
- for (int i = 1; i <= n; i++) {
- int number = Integer.parseInt(scanner.nextLine());
- if (number < 200) {
- p1 += 1;
- } else if (number < 400) {
- p2 += 1;
- } else if (number < 600) {
- p3 += 1;
- } else if (number < 800) {
- p4 += 1;
- } else {
- p5 += 1;
- }
- }
- for (int p : new int[]{p1, p2, p3, p4, p5}) {
- System.out.printf("%.2f%%\n", (double) p / n * 100);
- }
- }
- }
- ИЛИ:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int[] histogram = new int[]{0, 0, 0, 0, 0};
- for (int i = 1; i <= n; i++) {
- int number = Integer.parseInt(scanner.nextLine());
- histogram[(number < 200 ? 0 : number < 400 ? 1 : number < 600 ? 2 : number < 800 ? 3 : 4)]++;
- }
- for (int p : histogram) {
- System.out.printf("%.2f%%\n", (double) p / n * 100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement