求一个数组中三个最大值的下标

    技术2022-05-11  16

    #include <iostream>using namespace std;

    int MaxNum[3]={0};

    void sort(float b[],int NUM){ int i=0; int j=0; float MaxTemp;     int Record=0; while(j<3) {  MaxTemp=b[0];  Record=0;  for(i=0;i<NUM;i++)  {   if(b[i]>MaxTemp)   {    Record=i;    MaxTemp=b[i];   }  }  b[Record]=0;  MaxNum[j]=Record;  j++; } }

    int  main(){ float a[5]={4.5,4.5,3.5,0.5,1.5};  sort(a,5);  for(int i=0;i<3;i++) {  cout<<MaxNum[i]<<endl; } return 0;}

    using System;using System.Collections.Generic;using System.Text;

    namespace ConsoleApplication1{    class Program    {        static void PrintArray(float[] b)        {            int i = 0;            int j = 0;            float MaxTemp;            float[] MaxNum = new float[3];

                int Record = 0;            while (j < 3)            {                MaxTemp = b[0];                Record = 0;                for (i = 0; i < b.Length; i++)                {                    if (b[i] > MaxTemp)                    {                        Record = i;                        MaxTemp = b[i];                    }                }                b[Record] = 0;                MaxNum[j] = Record;                j++;            }                        //打印数组元素,arr.Length 表示数组元素的个数            for ( i = 0; i < 3; i++)            {                Console.WriteLine("MaxNum[{0}]={1}", i, MaxNum[i]);            }

            }

            static void Main(string[] args)        {            float[] numbers = new float[5] { 4.5F, 4.5F, 5F, 1F, 2F };            PrintArray( numbers);        }    }}


    最新回复(0)