(指针)输入10个整数, 把最小的与第一个数交换, 最大的与最后一个数交换

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
void input(int *num)       //输入10个数的函数
{
    int i;
    printf("input 10 numbers:");
    for(i=0; i<10; i++){
        scanf("%d", &num[i]);
    }
}
void swap(int *num)
{
    int *max, *min, *p, temp;
    max = min = num;        //开始时使max和min都指向第一个数
    for(p = num+1; p < num+10; p++){
        if(*p > *max)    max = p;    //若p指向的数大于max指向的数,就使max指向p指向的大数
        else if(*p < *min)    min = p;    //若p指向的数小于min指向的数,就使min指向p指向的小数
    }
    temp = num[0];
    num[0] = *min;
    *min = temp;    //将最小数与第一个数num[0]交换
    if(max == num)  max = min;  //若max和num相等,表示第一个数是最大数,则使max指向当前的最大数
    temp = num[9];
    num[9] = *max;
    *max = temp;    //将最大数与最后一个数num[9]交换
}
void output(int *num)
{
    int *p;
    printf("Now, they are:");
    for(p = num; p < num+10; p++){
        printf("%d ", *p);
    }
    printf("\n");
}
int main()
{
    int num[10];
    input(num);
    swap(num);
    output(num);
    return 0;
}
  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值