13、5个数求最值
设计一个从5个整数中取最小数和最大数的程序
- 输入 输入只有一组测试数据,为五个不大于1万的正整数
- 输出 输出两个数,第一个为这五个数中的最小值,第二个为这五个数中的最大值,两个数字以空格格开。
1、思路
建立数组,初始设第一个为最大值与最小值,遍历比较
2、具体实现
#include<iostream>
using namespace std;
int main()
{
int a[5] = { 0 };
for (int& it : a)cin >> it;
int max = a[0], min = a[0];
for (int it : a)
{
if (it > max)max = it;
if (it < min)min = it;
}
cout << "最大值为:" << max << "最小值为:" << min;
return 0;
}
14 、ASCII码排序
输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。
- 输入 第一行输入一个数N,表示有N组测试数据。后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,之间无空格。
- 输出 对于每组输入数据,输出一行,字符中间用一个空格分开。
1、思路
2、具体实现
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 0;
string str;
char tmp;
cin >> n;
cin.get();
while (n-- && getline(cin, str))
{
if (str[0] > str[1]) tmp = str[0], str[0] = str[1], str[1] = tmp;
if (str[0] > str[2]) tmp = str[0], str[0] = str[2], str[2] = tmp;
if (str[1] > str[2]) tmp = str[1], str[1] = str[2], str[2] = tmp;
cout << "从小到大为:" << str[0] << ' ' << str[1] << ' ' << str[2] << endl;
}
return 0;
}