前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第七章第五题(打印不同的教)(Print distinct numbers) - 编程练习题答案

第七章第五题(打印不同的教)(Print distinct numbers) - 编程练习题答案

作者头像
无刺鱼
发布2022-03-29 13:15:31
2280
发布2022-03-29 13:15:31
举报
文章被收录于专栏:许唯宇

编写一个程序,读人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.

下面是参考答案代码:

代码语言:javascript
复制
// 缺陷版本
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+
                "\nThe distinct numbers are: ");

        for (int i = 0; i < 10; i++) {
            if (distinctList[i] != -1) {
                System.out.print(distinctList[i] + " ");
            }
        }
    }
}

// 完善版本
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 maxInt = -99;
        for (int i = 0; i < 10; i++) {
            numberList[i] = input.nextInt();
            if (numberList[i] > maxInt)
                maxInt = numberList[i];
        }

        int[] distinctList = new int[maxInt];
        Arrays.fill(distinctList, -99);

        int disNum = 0;
        for (int i = 0; i < maxInt; i++) {
            for (int j = 0; j < 10; j++) {
                if (i == numberList[j])
                    distinctList[i] = numberList[j];
                else if (distinctList[i] != -99)
                    disNum = distinctList[i];
            }
        }

        int count = 0;
        for (int i = 0; i < maxInt; i++) {
            if (distinctList[i] != -99) {
                count++;
            }
        }
        System.out.print("The number of distinct number is " + disNum +
                "\nThe distinct numbers are: ");

        for (int i = 0; i < maxInt; i++) {
            if (distinctList[i] != -99) {
                System.out.print(distinctList[i] + " ");
            }
        }
        System.out.print(maxInt);
    }
}

//        Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
//        The number of distinct number is 5
//        The distinct numbers are: 1 2 3 4 5 6

//        Enter ten numbers: 11 22 33 22 11 66 33 44 55 67
//        The number of distinct number is 66
//        The distinct numbers are: 11 22 33 44 55 66 67
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档