顺序逆序编程
发布时间: 2023-11-12 10:12:39
① c语言编程:将一个数组中的值按逆序重新存放,例如,原来顺序为8,6,5,4,3,要求改为3,4,5
例:
#include<stdio.h>
int main()
{
char arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int i = 0;
int len = sizeof(arr) / sizeof(arr[0]);
int tmp = 0;
for (i = 0; i < (len/2); ++i) //进行len/2次交换
{
tmp = arr[9 - i];
arr[9 - i] = arr[i];
arr[i] = tmp;
}
for (i = 0; i < len; ++i)
{
printf(" %d ", arr[i]); //输出
}
return 0;
}
(1)顺序逆序编程扩展阅读
将一个数组中的值按照逆序重新存放
#include<stdio.h>
intmain()
{
inta[100];
intm;
inti=0,n=0,temp;
while(scanf("%d",&m)!=EOF&&n<100){
a[i++]=m;
n++;
}
i=0;
while(i<n/2){
temp=a[n-1-i];
a[n-1-i]=a[i];
a[i]=temp;
i++;
}
for(i=0;i<n;i++)
printf("%d",a[i]);
putchar(10);
return0;
}