输出一个数组中最小的三个数值,没有重复数值的,
分别输出到text1,text2,text3中。
(将数组排序我会,就是不知怎样去除重复的数??)参考数据:
数组:(21,3,34,5,7,5,14,17,11,13)输出结果:
以上数组中最小的三个数值是“3,5,7”.
而不是 “3,5,5”请写出代码来,谢谢!
分别输出到text1,text2,text3中。
(将数组排序我会,就是不知怎样去除重复的数??)参考数据:
数组:(21,3,34,5,7,5,14,17,11,13)输出结果:
以上数组中最小的三个数值是“3,5,7”.
而不是 “3,5,5”请写出代码来,谢谢!
假设排序后的数组为vArray,排序结果是从大到小
Dim vPrevValue,vCount
vPrevValue = -100
vCount = 0
For i=UBound(vArray) To 1 Step -1
If vArray(i)<>vPrevValue Then
Debug.Print vArray(i)
vPrevValue = vArray(i)
vCount=vCount+1
If vCount >=3 Then Exit For
End If
Next
Dim Result(1 To 3) As Long '结果数据,即为所求
'从小到大排序后得到(楼主会排序我就不罗嗦了)
myArray(1) = 3: myArray(2) = 5: myArray(3) = 5: myArray(4) = 7: myArray(5) = 11
myArray(6) = 13: myArray(7) = 14: myArray(8) = 17: myArray(9) = 21: myArray(10) = 21
Dim i As Long, j As Long
j = 1
Result(1) = myArray(1)
For i = 2 To 10
If myArray(i) > Result(j) Then
j = j + 1
Result(j) = myArray(i)
If j = 3 Then Exit For
End If
Next i
'Result数组值输出到文本框略
将数据从小到大排序,头三个不就是你要的??
问题解决了。
给大家介绍下 .NET LINQ 如何做。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = { 21, 3, 34, 5, 7, 5, 14, 17, 11, 13 };
var result = (from n in a orderby n select n).Distinct().Take(3).ToList();
foreach (int i in result)
Console.WriteLine(i);
}
}
}运行:
3
5
7大家看到,真正的代码只有1行。更强大的是,PLINQ是直接支持多线程并行的。在很多很多数据的场合,速度会非常快。VB.NET 也有类似的语法。不过我不熟。