int v[]={4,3,5,6,8,9,7,2,10,1}; int x1=v[0],b1=0;//b1下标 int x2=v[0],b2=0; int x3=v[0],b3=0;//x1=>x2=>x3 int b; for(int loop=1,lmax=v.length;loop<lmax;loop++){ b = v[loop]; if(b<x1){ if(b<x2){ x1=x2; b1=b2; if(b<x3){ x2=x3; b2=b3; x3=b; b3=loop; }else{ x2=b; b2=loop; } }else{ x1=b; b1=loop; } } }
x1 x2 x3是最小值 b1 b2 b3是对应下标
class Program { static void Main(string[] args) { int[] a ={ 20, 12, 25, 32, 56, 58, 14, 65, 98, 10 }; int[] b = { 20, 12, 25, 32, 56, 58, 14, 65, 98, 10 }; Array.Sort(a); int i, j=0; while (j<3) { for (i = 0; i < 10; i++) { if (b[i] != a[j]) continue; else Console.WriteLine("a[{0}]=={1}", i, a[j]); j++; } } Console.ReadLine(); } } } 这是我刚刚写的一个,希望对你又帮助!
class Program { static void Main(string[] args) { List<MyValue> lists = new List<MyValue>(); int[] arr = new int[8] { 12, 58, 65, 44, 7, 1, 98, 54 }; for (int i = 0; i < arr.Length; i++) { lists.Add(new MyValue(arr[i], i)); } for (int i = 0; i < lists.Count; i++) { for (int j = i + 1; j < lists.Count; j++) { if (lists[i].Value < lists[j].Value) { MyValue value1 = lists[j]; lists[j] = lists[i]; lists[i] = value1; } } } Console.WriteLine("最小的三个值为:"); for (int i = lists.Count-1; i >=lists.Count-3; i--) { Console.WriteLine("第{0}位:{1}",lists[i].Position,lists[i].Value); } Console.ReadLine(); } } class MyValue { public MyValue(int value, int position) { this._value = value; this._position = position; } private int _value; private int _position; public int Value { get { return _value; } set { _value = value; } } public int Position { get { return _position; } set { _position = value; } } }
Arrays.sort(array);
取索引为0,1,2的就行了
int x1=v[0],b1=0;//b1下标
int x2=v[0],b2=0;
int x3=v[0],b3=0;//x1=>x2=>x3
int b;
for(int loop=1,lmax=v.length;loop<lmax;loop++){
b = v[loop];
if(b<x1){
if(b<x2){
x1=x2;
b1=b2;
if(b<x3){
x2=x3;
b2=b3;
x3=b;
b3=loop;
}else{
x2=b;
b2=loop;
}
}else{
x1=b;
b1=loop;
}
}
}
b1 b2 b3是对应下标
{
static void Main(string[] args)
{
int[] a ={ 20, 12, 25, 32, 56, 58, 14, 65, 98, 10 };
int[] b = { 20, 12, 25, 32, 56, 58, 14, 65, 98, 10 };
Array.Sort(a);
int i, j=0;
while (j<3)
{
for (i = 0; i < 10; i++)
{
if (b[i] != a[j])
continue;
else
Console.WriteLine("a[{0}]=={1}", i, a[j]);
j++;
}
}
Console.ReadLine();
}
}
} 这是我刚刚写的一个,希望对你又帮助!
class Program
{
static void Main(string[] args)
{
List<MyValue> lists = new List<MyValue>();
int[] arr = new int[8] { 12, 58, 65, 44, 7, 1, 98, 54 };
for (int i = 0; i < arr.Length; i++)
{
lists.Add(new MyValue(arr[i], i));
}
for (int i = 0; i < lists.Count; i++)
{
for (int j = i + 1; j < lists.Count; j++)
{
if (lists[i].Value < lists[j].Value)
{
MyValue value1 = lists[j];
lists[j] = lists[i];
lists[i] = value1;
}
}
}
Console.WriteLine("最小的三个值为:");
for (int i = lists.Count-1; i >=lists.Count-3; i--)
{
Console.WriteLine("第{0}位:{1}",lists[i].Position,lists[i].Value);
}
Console.ReadLine();
}
} class MyValue
{
public MyValue(int value, int position)
{
this._value = value;
this._position = position;
} private int _value;
private int _position; public int Value
{
get
{ return _value; }
set
{ _value = value; }
} public int Position
{
get
{ return _position; }
set
{ _position = value; }
}
}