洛谷 P5718:找最小值 ← if + while
【题目来源】
https://www.luogu.com.cn/problem/P5718
【题目描述】
给出 n 和 n 个整数 ai,求这 n 个整数中最小值是什么。
【输入格式】
第一行输入一个正整数 n,表示数字个数。
第二行输入 n 个非负整数,表示 a1, a2, …, an,以空格隔开。
【输出格式】
输出一个非负整数,表示这 n 个非负整数中的最小值。
【输入样例】
5
5 7 4 2 6
【输出样例】
2
【数据范围】
数据保证,n≤100 且 0≤ai≤1000。
【算法分析】
本题考查选择结构及循环结构的用法。
【算法代码】
#include <bits/stdc++.h>
using namespace std;int imin=0x3f3f3f3f;
int n,x;int main() { cin>>n;while(n--){cin>>x;if(x<imin) imin=x;}cout<<imin<<endl;return 0;
}/*
in:
5
5 7 4 2 6out:
2
*/
【参考文献】
https://www.luogu.com.cn/problem/solution/P5718