#include <iostream> #include <windows.h> #include "stdlib.h" #include "time.h" using namespace std; void func(int a[], int n, int K, int (&max)[2]); void main() { srand(time(0)); int a[10]; int array[2]; for (int i = 0;i<10;i++) { a[i]= rand(); } func(a, 10, 2,array); cout<<array[0]<<endl; cout<<array[1]<<endl; } void func(int a[], int n, int K, int (&max)[2]) { int index; int i = 0; for (int j = 0;j<K;j++) { max[j] = *(a+0); /*find the max data*/ for(i=0;i<n;i++) { if (max[j] < *(a+i)) { max[j] = *(a+i); index = j; } } /*finish*/ a[index] = a[n-1]; n = n-1; } }
以上是方法1