如果lz不懂算法,或者懒得想这些,懒人推荐 C# 或者 VB.NET,因为有 LINQ。 看代码: using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] array = new int[] { 12, 3, 7, 4, 2, 5, 3, 9, 11, 14, 8, 21, 1, 17, 12, 18, 21, 20, 7, 6, 9, 30 }; int near = 10; var result = (from x in array select new { Key = x, Value = Math.Abs(x - near) }).OrderBy(x => x.Value); result.ToList().ForEach(x => Console.Write(x.Key + " ")); } } }9 11 9 12 8 12 7 7 14 6 5 4 3 3 17 2 18 1 20 21 21 30
数字很大?using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[1000000]; for (int i = 0; i < 1000000; i++) array[i] = r.Next(0, 2147483647); int near = 12345678; // 假设我们找最接近12345678的数。 var result = (from x in array.AsParallel() select new { Key = x, Value = Math.Abs(x - near) }).OrderBy(x => x.Value).Take(10); // 太多了,按照接近程度 take 10 个。 result.ToList().ForEach(x => Console.Write(x.Key + " ")); } } } 随即产生100万个整数,多不多?在P4 2.4C上运行,只需要2秒:12345758 12346044 12349297 12341959 12352777 12352867 12335509 12333437 12332691 12332303
没理解错的话,一个循环就够了吧: dim a dim n as long dim Idx as long,dim i as long
a=array(-2,-30,1,3,5,9,10,34,21,11,15,-15,-6) n=16 '找最接近16的数 for i=1 to ubound(a) if abs(n-a(i))< abs(n- a(Idx)) then Idx=i next debug.? Idx; a(Idx) '输出最接近的值的数组下标和值 机器上没vb,没测试,一种思路吧....
看代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 12, 3, 7, 4, 2, 5, 3, 9, 11, 14, 8, 21, 1, 17, 12, 18, 21, 20, 7, 6, 9, 30 };
int near = 10;
var result = (from x in array select new { Key = x, Value = Math.Abs(x - near) }).OrderBy(x => x.Value);
result.ToList().ForEach(x => Console.Write(x.Key + " "));
}
}
}9 11 9 12 8 12 7 7 14 6 5 4 3 3 17 2 18 1 20 21 21 30
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random r = new Random();
int[] array = new int[1000000];
for (int i = 0; i < 1000000; i++)
array[i] = r.Next(0, 2147483647);
int near = 12345678; // 假设我们找最接近12345678的数。
var result = (from x in array.AsParallel() select new { Key = x, Value = Math.Abs(x - near) }).OrderBy(x => x.Value).Take(10); // 太多了,按照接近程度 take 10 个。
result.ToList().ForEach(x => Console.Write(x.Key + " "));
}
}
}
随即产生100万个整数,多不多?在P4 2.4C上运行,只需要2秒:12345758 12346044 12349297 12341959 12352777 12352867 12335509 12333437 12332691
12332303
dim n as long
dim Idx as long,dim i as long
a=array(-2,-30,1,3,5,9,10,34,21,11,15,-15,-6)
n=16 '找最接近16的数
for i=1 to ubound(a)
if abs(n-a(i))< abs(n- a(Idx)) then Idx=i
next
debug.? Idx; a(Idx) '输出最接近的值的数组下标和值
机器上没vb,没测试,一种思路吧....