7-1 将数组中的数逆序存放 (15 分)

7-1 将数组中的数逆序存放 (15 分)
本题要求编写程序,将给定的n个整数存入数组中,将数组中的这n个数逆序存放,再按顺序输出数组中的元素。

输入格式:
输入在第一行中给出一个正整数n(1≤n≤10)。第二行输入n个整数,用空格分开。

输出格式:
在一行中输出这n个整数的处理结果,相邻数字中间用一个空格分开,行末不得有多余空格。

输入样例:
4
10 8 1 2
输出样例:
2 1 8 10

#include<iostream>
using namespace std;
void input(int[],int);
void reverse(int[],int);
void ouput(int[],int);
int main()
{	int a[10];
    int n;
    cin >> n ;
    input(a,n);     //读入n个数到a数组中
    reverse(a,n);  //交换a[i]与a[j]的值
    ouput(a,n);      //输出a[0,n-1]
    return 0; 
}
void input(int a[],int n)
{//读入n个数到a数组中
    for(int i=0;i<n;i++)
    cin >> a[i];
}  
void reverse(int a[],int n)
 {
    int i,j,t;
    for(i=0,j=n-1;i<j;i++,j--){
        //交换a[i]与a[j]的值
    t=a[i];
    a[i]=a[j];
    a[j]=t;
    }
 }
void ouput(int a[],int n)
{
    for(int i=0;i<n-1;i++) //输出前n-1个数
    cout << a[i] <<" ";

    cout <<a[n-1];
}
发布了12 篇原创文章 · 获赞 0 · 访问量 1218

猜你喜欢

转载自blog.csdn.net/weixin_45644335/article/details/102956377