English 中文(简体)
如何分类数字
原标题:How to sort numbers
  • 时间:2010-04-20 07:44:56
  •  标签:
  • c#
  • sorting

我需要一些帮助才能处理以下逻辑。 该方案得到许多分类输入,如<代码>10,16,3,17,21,29,6<>。

需遵循的逻辑是:

设想1:

首先选择最多的4项投入,即<代码>16,17,21,29。 现在将价值观分配给A、B、C和D:

  • A = smallest in the selected 4
  • B = biggest in the selected 4
  • C = second smallest in the selected 4
  • D = third smallest in the selected 4

显示:

A = 16
B = 29
C = 17
D = 21

设想:2

如果用户只向A、B、C转让3项投入,如<代码>3,6,10,而忽略D

显示:

A = 3
B = 10
C = 6
最佳回答
问题回答

I would take the input and store them into a List. I would then sort this list. Once sorted you can then pull out the numbers as required.

逻辑:

  1. Sort in descending order
  2. Select top 4 element
  3. Assign value as per your requirement in A,B,C,D

象这样的声音可能是一种派任,因此,我要告诉你:

如果你在一阵列中拥有所有这些数目,你就可以尝试部分地划分阵列。 书写简单易碎,其数量最多。 但是,在把4个最大的要素带至阵列前线之后,不要把整个阵列混为一谈。

这样,你的第一个要素将是你最大的。 从那里可以很容易地做你所需要的其他事情。

帮助的希望。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签