将整数数组排序为4& 5次表C#

时间:2015-07-21 18:14:22

标签: c# arrays linq sorting

我想知道解决这个问题的最佳方法。我有一个整数数组(比如3,4,8,10,15,24,29,30),我想把它分成3组:4次表,5次表,都没有。)

正如小组所建议的那样,它会将数组分为4倍和5倍的表格,而另一张表格则不存在于其中的任何一个。

在C#中解决此问题的最佳方法是什么?我目前正在使用这个:

class Item implements Comparable<Item> {

    int itemNumber;
    int price;
    String mango;
    String apple;
    String bannana;

    public Item(int itemNumber, int price, String mango, String apple, String bannana) {
        this.itemNumber = itemNumber;
        this.price = price;
        this.mango = mango;
        this.apple = apple;
        this.bannana = bannana;
    }

    //GETTERS

    @Override
    public int compareTo(Item compareItem) {
        int comparePrice = ((Item) compareItem).getPrice();
        //ascending order
        //return this.price - comparePrice;

        //descending order
        return comparePrice - this.price;
    }

}

public static void main(String[] args) {
    List<Item> items = new ArrayList<>();

    //populate the items list by creating an Item for every line you read.
    //Handle null price values
    Collections.sort(items);

    //assuming input is some like 'Mango/purchased/top50'
    String input = "Mango/purchased/top50";
    String[] parts = input.split("/");
    int max = Integer.parseInt(parts[2].substring(3));

    List<Item> result = new ArrayList<>();
    for (int i = 0; i < items.size() && result.size() < max; i++) {
        Item item = items.get(i);
        if ((parts[0].equals("Mango") && item.getMango().equals(parts[1]))
                || (parts[0].equals("Apple") && item.getApple().equals(parts[1]))
                || (parts[0].equals("Bannana") && item.getBannana().equals(parts[1]))) {
            result.add(item);
        }
    }
}

制作:

4

4 8 24

5

10 15 30

其他

3 29

3 个答案:

答案 0 :(得分:0)

GetString(5)

如果你想得到所有 4的倍数和所有 5的倍数(并且两者之间有一些重叠),你可以这样做:

int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

但是,如果您不想重叠,则需要选择哪一个占优势。我在这里选择了4:

List<int> multiplesOf4 = (from i in arr where i % 4 == 0 select i).ToList();
List<int> multiplesOf5 = (from i in arr where i % 5 == 0 select i).ToList();
List<int> others = (from i in arr where i % 5 != 0 && i % 4 != 0 select i).ToList();

答案 1 :(得分:0)

试试这个:

var numberGroupsTimes5 =
            from n in numbers
            group n by n % 5 into g
            where g.Key == 0
            select new { Remainder = g.Key, Numbers = g };

var numberGroupsTimes4 =
            from n in numbers
            group n by n % 4 into g
            where g.Key == 0
            select new { Remainder = g.Key, Numbers = g };

foreach (var g in numberGroupsTimes5)
{
     string st = string.Format("Numbers with a remainder of {0} when divided by 5:" , g.Remainder);
     MessageBox.Show("" + st);
     foreach (var n in g.Numbers)
     {
          MessageBox.Show(""+n);
     }
} 
foreach (var g in numberGroupsTimes4)
{
     string st = string.Format("Numbers with a remainder of {0} when divided by 4:", g.Remainder);
     MessageBox.Show("" + st);
     foreach (var n in g.Numbers)
     {
         MessageBox.Show("" + n);
     }
} 

答案 2 :(得分:0)

你接近是正确的。但是你可以做一些小的改进,使它更具可读性和标准性:

var iArray = new[] { 3, 4, 8, 10, 15, 24, 29, 30 };//Don't need to give type(int) explicitly
var iE = iArray.GroupBy(e => e % 4 == 0 ? "four" : e % 5 == 0 ? "five" : "other").OrderBy(e => e.Count());

它会给出相同的结果。