OJ click "input 10 integers" and oj click "input 10 integers"
Description
Enter 10 Integers to replace the minimum number with the first number, and the maximum number with the last number. Write three functions; ① input 10 numbers; ② process; ③ output 10 numbers.
Input
10 Integers
Output
After sorting, there are 10 digits, each followed by a space (note that there is a space after the last number)
Sample Input
2 1 3 4 5 6 7 8 10 9
Sample output
1 2 3 4 5 6 7 8 9 10
Prompt
The main function has been given as follows, and does not need to include the following main function when submitting
/* C/C ++ Code */
Int main ()
{
Const int n = 10;
Int a [n];
Input (a, n );
Handle (a, n );
Output (a, n );
Return 0;
}
The Code is as follows:
#include <iostream>using namespace std;void input(int *a,int n){ int i=0; while (i<n) { cin>>*(a+i); i++; }}void handle (int *a,int n){ int i=0,max=*(a+n-1),min=*a,t,k1,k2; while (i<n) { if (*(a+i)>max) { max=*(a+i); k1=i; } else if(*(a+i)<min) { min=*(a+i); k2=i; } i++; } t=*(a+k1); *(a+k1)=*(a+n-1); *(a+n-1)=t; t=*(a+k2); *(a+k2)=*a; *a=t; *(a+n)='\0';}void output(int *a,int n){ int i; for(i=0;*a!='\0';i++) { cout<<*a<<" "; a++; }}int main(){ const int n=10; int a[n]; input(a,n); handle(a,n); output(a,n); return 0;}
You still cannot submit the image. Be sure to pay attention to the pointer boundary. The cin> * a; a ++; I ++ used in the input function then accesses the unknown address, the running error is displayed after submission, and * (a + n) = '\ 0' is the problem. Remember, otherwise there will be more numbers in the output.