c语言中利用对象宏式对数组元素进行倒序排列

 

1、

#include <stdio.h>
#define NUMBER 5

int main(void)
{
    int a[NUMBER];
    int i;
    puts("please input several numbers.");
    
    for (i = 0; i < NUMBER; i++)
    {
        printf("%dst = ", i + 1);
        scanf("%d", &a[i]);
    }
    puts("-----\n-----\n-----");
    
    int temp;
    for (i = 0; i < NUMBER/2; i++)
    {
        temp = a[i];
        a[i] = a[NUMBER-1-i];
        a[NUMBER-1-i] = temp;
    }
    
    for (i = 0; i < NUMBER; i++)
        printf("a[%d] = %d\n", i, a[i]);
    return 0;
}

 

 

 

2、

#include <stdio.h>
#define NUMBER 8

int main(void)
{
    int a[NUMBER];
    int i;
    puts("please input several numbers.");
    
    for (i = 0; i < NUMBER; i++)
    {
        printf("%dst = ", i + 1);
        scanf("%d", &a[i]);
    }
    puts("-----\n-----\n-----");
    
    int temp;
    for (i = 0; i < NUMBER/2; i++)
    {
        temp = a[i];
        a[i] = a[NUMBER-1-i];
        a[NUMBER-1-i] = temp;
    }
    
    for (i = 0; i < NUMBER; i++)
        printf("a[%d] = %d\n", i, a[i]);
    return 0;
}

 

posted @ 2021-02-04 00:04  小鲨鱼2018  阅读(148)  评论(0编辑  收藏  举报