列出前3个销售数量

时间:2014-01-03 06:40:49

标签: c#

问题 - 我需要列出QTY中出售的前三条鱼,但我无法弄清楚代码。

这就是我到目前为止所做的.....我可能知道我的情况不对或我的循环错了吗?

private void btnMax_Click(object sender, EventArgs e) //Allan
            {
            //Local Variable
            float max_value1 = sales_qty[0];
            float max_value2 = sales_qty[0];
            float max_value3 = sales_qty[0];
            int max_index1 = 0;
            int max_index2 = 0; 
            int max_index3 = 0;

            //Loop to find Highest Qty of Fish Sold
            for (int i = 1; i < fish_name.Length; i++)
            {
                if (sales_qty[i] > max_value1)
                {
                    max_value1 = sales_qty[i];
                    max_index1 = i;
                }
            }

            for (int i = 1; i < fish_name.Length; i++)
            {
                if (sales_qty[i] > max_value2 && sales_qty[i] != max_value1)
                {
                    max_value2 = sales_qty[i];
                    max_index2 = i;
                }
            }

            for (int i = 1; i < fish_name.Length; i++)
            {
                if (sales_qty[i] > max_value3 && sales_qty[i] != max_value2)
                {
                    max_value3 = sales_qty[i];
                    max_index3 = i;
                }
            }

            //Display
            MessageBox.Show("Top 3 Selling Fish is: "
                + "\n"
                + fish_name[max_index1]
                + "Quantity Sold:"
                + sales_qty[max_index1]
                + "\n"
                + fish_name[max_index2]
                + "Quantity Sold:"
                + sales_qty[max_index2]
                + "\n"
                + fish_name[max_index3]
                + "Quantity Sold:"
                + sales_qty[max_index3]);

        }

2 个答案:

答案 0 :(得分:1)

首先,你的启动是错误的:

float max_value1 = sales_qty[0];
float max_value2 = sales_qty[0];
float max_value3 = sales_qty[0];

如果sales_qty [0]是最大元素怎么办?然后,以下所有条件都将返回错误。然后,你找不到第二高和第三高的元素:

if (sales_qty[i] > max_value2 && sales_qty[i] != max_value1)
if (sales_qty[i] > max_value3 && sales_qty[i] != max_value2) 

接下来,您的条件暗示您假设这些值不同,对吧? 然而,实际上前三个数量可以相等:D。

要确保这些数量属于不同类型的鱼类,您应该检查索引,而不是值。

这是我建议的检查条件的代码。您可以用它替换支票:)

        int[] sales_qty  = {10,1,3,0,2,10,9};//just for testing. You can disable it
        float max_value1 =-1;
        float max_value2 = -1;
        float max_value3 = -1;
        int max_index1 = -1;
        int max_index2 = -1; 
        int max_index3 = -1;

        //Loop to find Highest Qty of Fish Sold
        //always ensure max_value1 >=  max_value2 >= max_value3;
        for (int i = 0; i < sales_qty.Length; i++)
        {
            if (sales_qty[i] > max_value1) {//need re-assign
                max_value3 = max_value2; max_index3 = max_index2; 
                max_value2 = max_value1; max_index2 = max_index1;
                max_value1 = sales_qty[i]; max_index1 = i; continue;}

            //mean sales_qty[i] <= max_value1;
            if (sales_qty[i] > max_value2) {//need re-assign
                max_value3 = max_value2; max_index3 = max_index2;
                max_value2 = sales_qty[i]; max_index2 = i; continue;}

            //mean sales_qty[i] <= max_value1 and max_value2;
            if (sales_qty[i] > max_value3) {max_value3 = sales_qty[i]; max_index3 = i; continue;}

        }

答案 1 :(得分:0)

当我曾经有一个5M +条目的数组并且想要找到可变数量(1到25)的最大值时,我使用了类似于以下内容:

int[] sales = {10,1,3,0,2,10,9};
int x=3; // Amount of top values.

int[] max=int[x]; // Contains the x max values (not indices)
for(int i=0;i<x;i++) max[i]=0;

int mini = 0; // Index of smallest value in max array (first to discard)

for(int i=0;i<sales.length;i++) {
 if(sales[i]>max[mini]) {
  max[mini]=sales[i]; // add new value, overwrite smallest max array entry
  for(int j=0;j<x;j++) if(max[j]<max[mini]) mini=j; // Find new minimum
 }
}

我不需要它,但你想要在之后对max数组进行排序。