#include<iostream> #include<vector> using namespace std; vector<int> inputFunction();//输入void processFunction(vector<int> &num);//处理void output(vector<int> &num);//输出int main(){ vector<int> numbers; numbers = inputFunction(); processFunction(numbers); output(numbers); return 0;}vector<int> inputFunction(){ vector<int> numbers; int buf; cout << "请输入10个整数" << endl; for(int i = 1; i <= 10; i++) { cout << "第" << i << "个数:"; cin >> buf; numbers.push_back(buf); } return numbers;}void processFunction(vector<int> &num){ int max, min, maxIndex, minIndex; max = num[0]; maxIndex = 0; min = num[0]; minIndex = 0; for(int i = 1; i < 10; i++) { if(max < num[i]) { max = num[i]; maxIndex = i; } if(min > num[i]) { min = num[i]; minIndex = i; } } //对换 num[minIndex] = num[0]; num[maxIndex] = num[9]; num[0] = min; num[9] = max;}void output(vector<int> &num){ for(int i = 0; i < 10; i++) { cout << num[i] << "\t"; }}
编译通过:输入:2 5 8 3 0 1 10 5 6 9 输出: 0 2 5 8 3 1 5 6 9 10 #include <stdio.h> int file(int a[10]) { int i; for(i=0;i<10;i++) scanf("%d",&a[i]); return 1; } exc(int a[10]) { int max=a[0],min=a[0],i,t,k,p; for(i=1;i<10;i++) { if(max<a[i]) {max=a[i];k=i;} if(min>a[i]) {min=a[i];p=i;} } {t=a[k];a[k]=a[9];a[9]=t;} {t=a[p];a[p]=a[0];a[0]=t;} return 1; } main() { int a[10]; int i; file(a); exc(a); for(i=0;i<10;i++) printf("%d ",a[i]); }