【问题标题】:How can I discard the largest and smallest numbers when I calculate the arithmetic mean?计算算术平均值时如何丢弃最大和最小的数字?
【发布时间】:2013-12-30 18:47:09
【问题描述】:

我的问题是关于在我的程序计算算术平均值时丢弃最大和最小的数字。

这是我的代码示例:

public static void main(String[] args) {
    int k;
    int r;
    int thelargest;
    int thesmallest;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the list of number : ");
    String input2 = input.nextLine();

    String[] numbers = input2.split(" ");

    int[] result = new int[numbers.length];
    for (r = 0; r < numbers.length; r++) {
        result[r] = Integer.parseInt(numbers[r]);

    }

    for (k = 0; k < result.length; k++) {
        System.out.print("");
        System.out.println(result[k]);
    }

    System.out.println(" LargestNumber :  " + TheLargestNumber(result));
    System.out.println(" SmallestNumber :  " + TheSmallestNumber(result));
    thelargest = TheLargestNumber(result);
    thesmallest = TheSmallestNumber(result);
    System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));

}

public static int TheSmallestNumber(int[] series) {
    int thesmallest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] < thesmallest) {

            thesmallest = series[i];
        }
    }
    return thesmallest;
}

public static int TheLargestNumber(int[] series) {
    int thelargest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] > thelargest) {

            thelargest = series[i];
        }
    }
    return thelargest;
}

public static float AirthmeticMean(int[] result) {
    int sum = 0;
    for (int i = 0; i < result.length; i++) {
        sum += result[i];
    }
    return (float) sum / result.length;
}

我试图找到方法并编写了此示例,但我不知道如何嵌入此代码示例:

         for (int i = 0; i < result.length; i++) {
        if (series[i] != thesmallest && series[i] != thelargest) {
            total = total + seriess[i];
        }
    }

此代码示例对我有帮助吗?

【问题讨论】:

  • 当您遍历循环添加数字时,检查最大和最小数字。然后在循环完成后,从总和中减去这些数字。像 PI 一样简单。
  • 你能打开它吗?因为我不明白该怎么做。在代码示例上显示对我有好处。@HovercraftFullOfEels
  • @Hovercraft Full Of Eels:如果最大或最小的数字出现多次,这将不起作用。
  • TheSmallestNumber 最好让函数名称以小写开头,例如 theSmallestNumber

标签: java math


【解决方案1】:

就在你之前

System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));

thenewmean = (AirthmeticMean(result)*result.length - thesmallest - thelargest)/(result.length-2)

然后打印thenewmean

System.out.println("The Arithmetic Mean : " + thenewmean);

你不需要写你的

for (int i = 0; i < result.length; i++) {
    if (series[i] != thesmallest && series[i] != thelargest) {
        total = total + seriess[i];
    }
}

在任何地方编码。即使那样,如果您想使用自己的代码, 然后在你的 AirthmeticMean() 函数中使用它

【讨论】:

  • 如果最大或最小数字出现多次,则您的答案无效。
  • 是的,真的……我没想到
【解决方案2】:

如果删除最高和最小,则需要跟踪计数和总和

public static float AirthmeticMean(int[] result, int theSmallest, int theLargest) {
    int sum = 0;
    int cnt = 0;
    for (int i = 0; i < result.length; i++) {
            if (result[i] != theSmallest && result[i] != theLargest) {
                sum += result[i];
                cnt++;
            }
    }
    return (float) sum / cnt;
}

【讨论】:

    【解决方案3】:

    你得到的代码示例:

    for (int i = 0; i < result.length; i++) {
        if (series[i] != thesmallest && series[i] != thelargest) {
            total = total + seriess[i];
        }
    }
    

    几乎没问题。几乎,因为您在 for 循环中检索名为 result 的数组的长度并访问数组 series 的元素。

    您可以通过以下方式使用此代码。使用两个参数 theSmallesttheLargest 扩展 AirthmeticMean 并跟踪求和元素的数量:

    public static float AirthmeticMean(int[] result, int theSmallest, int theLargest) {
        int sum = 0;
        int numElements = 0;
        for (int i = 0; i < result.length; i++) {
            if (result[i] != theSmallest && result[i] != theLargest) {
                sum += result[i];
                numElements++;
            }
        }
        return (float) sum / numElements;
    }
    

    编辑:添加numElements

    【讨论】:

    • 如果最大或最小数字多次出现,则最后一个提案不起作用。 (海报编辑:它有一个解决方案,它从总和中减去最小和最大的数字一次。)
    • 谢谢你,我的朋友。它有效。我能再问你最后一件事吗?我可以使用 ACM 库(ConsoleProgram)运行这个程序吗?你有这方面的任何信息吗?
    • @BarryNL 我删除了这个建议。这取决于您是完全丢弃最小和最大的数字还是只丢弃一次。我会说这是定义问题:)
    • @mertha 抱歉,我无法帮助您处理 ACM 库。
    • 数组列表 -> 删除所有出现的最小和最大
    猜你喜欢
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2016-11-04
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多