zoukankan      html  css  js  c++  java
  • 第七章第五题(打印不同的教)(Print distinct numbers)

    编写一个程序,读人10 个数并且显示互不相同的数(即一个数出现多次,但仅显示一次)。(提示,读人一个数,如果它是一个新数,则将它存储在数组中。如果该数已经在数组中,则忽略它。)输入之后,数组包含的都是不同的数。下面是这个程序的运行示例:

    Write a program that reads in ten numbers and displays
    the number of distinct numbers and the distinct numbers separated by exactly one
    space (i.e., if a number appears multiple times, it is displayed only once). (Hint:
    Read a number and store it to an array if it is new. If the number is already in the
    array, ignore it.) After the input, the array contains the distinct numbers.

    下面是参考答案代码:

    // https://cn.fankuiba.com
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Ans7_5_page236 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter ten numbers: ");
            int[] numberList = new int[10];
            // int[] distinctList = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
            int[] distinctList = new int[10];
            Arrays.fill(distinctList,-1);
    
            for (int i = 0; i < 10; i++)
                numberList[i] = input.nextInt();
    
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10;j++) {
                    if (i == numberList[j])
                        distinctList[i] = numberList[j];
                }
            }
    
            int count = 0;
            for (int i = 0; i < 10; i++) {
                if (distinctList[i] != -1)
                    count++;
            }
            System.out.print("The number of distinct number is " + count+
                    "
    The distinct numbers are: ");
    
            for (int i = 0; i < 10; i++) {
                if (distinctList[i] != -1) {
                    System.out.print(distinctList[i] + " ");
                }
            }
        }
    }
    

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

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

  • 相关阅读:
    input File 提交文件上传 无法传递数据的原因
    谈谈运维该掌握那些技能
    虚拟机vmware 下Centos 系统磁盘扩容
    jenkins针对不同的项目组对用户进行权限分配
    Mac Catalina 手残误删系统文件恢复过程
    自动化持续集成
    zabbix 监控之主动配置模式
    nginx对请求后缀或IP访问限制
    elasticsearch7.2.1 报错 Received fatal alert: handshake_failure
    使用nexus创建npm私服
  • 原文地址:https://www.cnblogs.com/in2013/p/13258473.html
Copyright © 2011-2022 走看看