• 第七章第三题(计算数字的出现次数)(Count occurrence of numbers)


    编写程序,读取在1到100 之间的整数,然后计算每个数出现的次数。假定输入是以0 结束的。

    下面是这个程序的一个运行示例:

    Write a program that reads the integers between 1and 100 and counts the occurrences of each. Assume the input ends with 0.Note that if a number occurs more than one time, the plural word “times” is used

    in the output.

    Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
    2 occurs 2 times
    3 occurs 1 time
    4 occurs 1 time
    5 occurs 2 times
    6 occurs 1 time
    23 occurs 1 time
    43 occurs 1 time
    

    下面是参考答案代码:

    // https://cn.fankuiba.com
    import java.util.Scanner;
    
    public class Ans7_3_page236 {
        public static void main(String[] args) {
            int[] number = new int [101];
            Scanner input = new Scanner(System.in);
            int num;
            System.out.print("Enter the integers between 1 and 100: ");
            do {
                num = input.nextInt();
                number[num] = number[num] + 1;
            }
            while (num != 0);
            for (int i = 1; i < number.length; i++) {
                if (number[i] == 1) {
                    System.out.println(i + " occurs " + number[i] + " time");
                }else if (number[i] > 1)
                    System.out.println(i + " occurs " + number[i] + " times");
            }
        }
    }
    

    适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)

    发布在博客:(https://cn.fankuiba.com)

  • 相关阅读:
    hdu-3376-Matrix Again(最小费用最大流)
    CF-164C. Machine Programming(最小费用最大流)
    splay模板
    POJ-3580-SuperMemo(splay的各种操作)
    pygame安装
    hg 证书验证失败
    hdu-3487-Play with Chain-(splay 区间翻转,切割,插入)
    jvm 重载 重写
    多线程踩坑
    hashmap时间复杂度
  • 原文地址:https://www.cnblogs.com/in2013/p/13232508.html
Copyright © 2020-2023  润新知