当前位置:网站首页>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]);
}
边栏推荐
猜你喜欢

二进制序列

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

IDEA2021.2安装与配置(持续更新)

MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql

A detailed explanation: SRv6 Policy model, calculation and drainage

win10重建索引

Rust编译报错:error: linker `cc` not found

Py's pdpbox: a detailed introduction to pdpbox, installation, and case application

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

QT 在父类中添加子类的流程,object tree,
随机推荐
Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
d使用among的问题
Installation and use of cnpm
语言代码表
打动中产精英群体,全新红旗H5用产品力跑赢需求
PhpMetrics 使用
MYSQL JDBC图书管理系统
IDEA2021.2安装与配置(持续更新)
MySQL进阶sql性能分析
2022.7.30
【科研】文献下载神器方式汇总
EasyExcel comprehensive course combat
Difference between cookie and session
MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
详解操作符
mysql锁机制
When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'
2022/07/30 学习笔记 (day20) 面试题积累
代码越写越乱?那是因为你没用责任链
DistSQL in-depth analysis: creating a dynamic distributed database