C++ 实例 - 判断三个数中的最大数
C++ 教程
· 2019-02-26 11:58:56
通过屏幕我们输入三个数字,并找出最大的数。
实例 - 使用 if
#include <iostream>
using namespace std;
int main()
{
float n1, n2, n3;
cout << "请输入三个数: ";
cin >> n1 >> n2 >> n3;
if(n1 >= n2 && n1 >= n3)
{
cout << "最大数为: " << n1;
}
if(n2 >= n1 && n2 >= n3)
{
cout << "最大数为: " << n2;
}
if(n3 >= n1 && n3 >= n2) {
cout << "最大数为: " << n3;
}
return 0;
}
以上程序执行输出结果为:
请输入三个数: 2.3 8.3 -4.2 最大数为: 8.3
实例 - 使用 if...else
#include <iostream>
using namespace std;
int main()
{
float n1, n2, n3;
cout << "请输入三个数: ";
cin >> n1 >> n2 >> n3;
if((n1 >= n2) && (n1 >= n3))
cout << "最大数为: " << n1;
else if ((n2 >= n1) && (n2 >= n3))
cout << "最大数为: " << n2;
else
cout << "最大数为: " << n3;
return 0;
}
以上程序执行输出结果为:
请输入三个数,以空格分隔: 2.3 8.3 -4.2 最大数为: 8.3
实例 - 使用内嵌的 if...else
#include <iostream>
using namespace std;
int main()
{
float n1, n2, n3;
cout << "请输入三个数: ";
cin >> n1 >> n2 >> n3;
if (n1 >= n2)
{
if (n1 >= n3)
cout << "最大数为: " << n1;
else
cout << "最大数为: " << n3;
}
else
{
if (n2 >= n3)
cout << "最大数为: " << n2;
else
cout << "最大数为: " << n3;
}
return 0;
}
以上程序执行输出结果为:
请输入三个数,以空格分隔: 2.3 8.3 -4.2 最大数为: 8.3
点击查看所有 C++ 教程 文章: https://www.codercto.com/courses/l/18.html
锦绣蓝图
[美] 沃德科 (Christina Wodtke)、[美] 戈夫拉 (Austin Govella) / 蔡芳 / 人民邮电出版社 / 2009-11-01 / 59.00
Web 2.0和社会化大趋势下,你的网站发展喜人,但是问题也接踵而来:信息变得越来越庞杂无序,业务流程愈加复杂,搜索和导航越来越难,用户对使用体验的要求也越来越高……怎么办? 作者非常通俗易懂地讲述了如何规划易用的网站及其背后的信息架构原理。首先介绍了建立信息架构的八项基本原则,然后重点强调了组织系统和元数据在信息架构中的作用,并指出设计搜索和导航需要考虑的问题和方法,另外还补充了当今热门的......一起来看看 《锦绣蓝图》 这本书的介绍吧!