Description
给出三个数a,b,c,最大值是?最小值是?
编写以下两个函数:
get_num()的功能是读取输入的三个整数a,b,c;
max_min()的功能是求出a,b,c的最大值和最小值。
以上函数的调用格式见“Append Code”。这里不给出函数原型,请通过main()函数自行确定。
Input
输入的第一个整数n,表示有n组测试数据,每组3个整数:a,b,c。a,b,c都在int类型范围内。
Output
每组测试数据对应输出一行:为a,b,c的最大值和最小值,格式见sample。
Sample Input
5
20 15 10
10 15 20
100 100 0
0 1 -1
0 0 0
Sample Output
case 1 : 20, 10
case 2 : 20, 10
case 3 : 100, 0
case 4 : 1, -1
case 5 : 0, 0
HINT
Append Code
append.c, append.cc,
int main()
{
int cases;
int mmax, mmin, a, b, c;
cin>>cases;
for(int i = 1; i <= cases; ++i)
{
get_num(a, b, c);
max_min(mmax, mmin, a, b, c);
cout<<"case "<<i<<" : "<<mmax<<", "<<mmin<<endl;
}
}
AC代码
#include <iostream>
using namespace std;
void get_num(int &a,int &b,int &c)//这里忘记了写int
{
cin>>a>>b>>c;
}
void max_min(int &mmax,int &mmin,int &a,int &b,int &c)//这里a,b,c前面的&号可有可无,因为其值没有改变;
{
mmax=max(a,b);//这里max只能一次比较两个数字;
mmax=max(mmax,c);
mmin=min(a,b);//这里min只能一次比较两个数字;
mmin=min(mmin,c);
}
int main()
{
int cases;
int mmax, mmin, a, b, c;
cin>>cases;
for(int i = 1; i <= cases; ++i)
{
get_num(a, b, c);
max_min(mmax, mmin, a, b, c);
cout<<"case "<<i<<" : "<<mmax<<", "<<mmin<<endl;
}
}
max与min函数
1、头文件#include algorithm
2、max的作用是比较出两者中的最大值,min的作用是比较出两者的最小值。
该博客介绍了如何编写两个C语言函数,`get_num()`用于读取三个整数输入,`max_min()`则用于计算并返回这三个数的最大值和最小值。提供了多个测试案例展示函数的正确输出,并提示可以使用`<algorithm>`库中的`max`和`min`函数来简化实现。
2472

被折叠的 条评论
为什么被折叠?



