练习7-3 将数组中的数逆序存放

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

输入格式:

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

输出格式:

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

输入样例:

4
10 8 1 2
 

输出样例:

2 1 8 10

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main(){
 4     int i,j,n,*a,*b,count=0;
 5     scanf("%d",&n);
 6     a=(int*)malloc(n*sizeof(int));
 7     b=(int*)malloc(n*sizeof(int));
 8     for(i=0;i<n;i++)
 9         scanf("%d",&a[i]);
10     for(j=n-1;j>=0;j--)
11     {
12         b[count]=a[j];
13         count++;
14     }
15     printf("%d",b[0]);
16     for(i=1;i<n;i++)
17     {
18         printf(" %d",b[i]);
19     }
20     printf("\n");
21 
22     return 0;
23 }

 

posted @ 2021-03-25 22:42  醉月8848  阅读(310)  评论(0)    收藏  举报