OJ click "input 10 integers" and oj click "input 10 integers"

Source: Internet
Author: User

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.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.