当前位置:网站首页>ZZULIOJ:1120: 最值交换
ZZULIOJ:1120: 最值交换
2022-07-30 22:46:00 【追上】
1120: 最值交换
时间限制: 1 Sec 内存限制: 128 MB
[状态] [讨论版] [提交] [命题人:admin]
题目描述
有一个长度为n的整数序列。请写一个程序,先把序列中的最小值与第一个数交换,再把最大值与最后一个数交换。输出转换好的序列。 分别编写两个函数MinIndex()和MaxIndex()来计算最小值下标和最大值下标。
int MinIndex(int a[], int n); //函数返回数组a中最小元素的下标
int MaxIndex(int a[], int n); //函数返回数组a中最大元素的下标
数组元素的输出调用函数PrintArr()。
输入
输入包括两行。
第一行为正整数n(1≤n≤10)。
第二行为n个正整数组成的序列,保证没有重复元素。
输出
输出转换好的序列。数据之间用空格隔开。
样例输入 Copy
5 5 4 1 2 3
样例输出 Copy
1 4 3 2 5
参考代码
#include <stdio.h>
#include <stdlib.h>
void PrintArr(int a[], int n);
int Minlndex(int a[], int n);
int Maxlndex(int a[], int n);
int insert(int a[], int n, int num);
int main()
{
int a[10], n, i, j, b;
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
j=Minlndex(a, n);
b=a[j];
a[j]=a[0];
a[0]=b;
j=Maxlndex(a, n);
b=a[j];
a[j]=a[n-1];
a[n-1]=b;
PrintArr(a, n);
return 0;
}
int Minlndex(int a[], int n)
{
int i, c=a[0], m=0;
for(i=1;i<n;i++)
{
if(a[i]<c)
{
c=a[i];
m=i;
}
}
return m;
}
int Maxlndex(int a[], int n)
{
int i, c=a[0], m=0;
for(i=1;i<n;i++)
{
if(a[i]>c)
{
c=a[i];
m=i;
}
}
return m;
}
void PrintArr(int a[], int n)
{
int i;
for(i=0;i<n;i++)
printf("%d ", a[i]);
}
边栏推荐
猜你喜欢

ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program

【云驻共创】HCSD大咖直播–就业指南

TCP 连接 三次握手 四次挥手

MySQL压缩包方式安装,傻瓜式教学

MySQL Soul 16 Questions, How Many Questions Can You Last?

史上最全的Redis基础+进阶项目实战总结笔记

Go1.18升级功能 - 泛型 从零开始Go语言

使用LVS和Keepalived搭建高可用负载均衡服务器集群

二进制序列

MySQL联合查询(多表查询)
随机推荐
Solve the problem of centos8 MySQL password ERROR 1820 (HY000) You must reset your password using the ALTER USER
MySQL Soul 16 Questions, How Many Questions Can You Last?
【云驻共创】HCSD大咖直播–就业指南
The problem of sticky packets in tcp protocol transmission
MYSQL JDBC图书管理系统
mysql跨库关联查询(dblink)
mysql锁机制
The mysql time field is set to the current time by default
Chapter 8 Intermediate Shell Tools II
【无标题】
win10重建索引
【Untitled】
Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)
力扣题(2)—— 两数相加
ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c
cookie和session区别
打动中产精英群体,全新红旗H5用产品力跑赢需求
language code table
PhpMetrics usage
2sk2225代换3A/1500V中文资料【PDF数据手册】