For the first question of the training plan, start with the water question: sort the array, and then output the middle number.
http://poj.org/problem?id=2388
Bubble Sort:
#include <iostream>using namespace Std;int main () { int i, J, N,t; int a[10000]; cin>>n; for (i=0; i<n; i++) { cin>>a[i]; } Bubble sort for (i=0; i<n-1; i++) for (j=0; j<n-i-1; j + +) if (a[j]<a[j+1]) { t=a[j]; A[J]=A[J+1]; a[j+1]=t; } cout<<a[n/2]<<endl; return 0;}
Quick sort:
#include <iostream>using namespace std;//quick sort void qsort (int a[], int l, int r) { int x=a[l], i=l, j=r; if (l>=r) return; while (i<j) { while (i<j && a[j]>=x) j--; A[I]=A[J]; while (i<j && a[i]<=x) i++; A[j]=a[i]; } a[i]=x; Qsort (a,l,i-1); Qsort (a,i+1,r);} int main () { int i, n; int a[10000]; cin>>n; for (i=0; i<n; i++) { cin>>a[i]; } Qsort (a,0,n-1); cout<<a[n/2]<<endl; return 0;}
Who's in the middle (simple sort)