用cpp写比较三个数大小
时间: 2023-05-14 17:06:06 浏览: 68
以下是用 C++ 写比较三个数大小的代码:
```cpp
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "请输入三个整数:" << endl;
cin >> a >> b >> c;
if (a > b) {
if (a > c) {
cout << "最大的数是:" << a << endl;
} else {
cout << "最大的数是:" << c << endl;
}
} else {
if (b > c) {
cout << "最大的数是:" << b << endl;
} else {
cout << "最大的数是:" << c << endl;
}
}
return 0;
}
```
希望能对您有所帮助。
相关问题
qt中快速比较多个数字的大小
在Qt中可以使用`qMax()`和`qMin()`函数快速比较多个数字的大小。这两个函数可以比较任意类型的变量,包括整型、浮点型等。
例如,比较三个整数a、b、c的最大值可以写成:
```cpp
int maxNum = qMax(qMax(a, b), c);
```
同理,比较三个浮点数的最小值可以写成:
```cpp
float minNum = qMin(qMin(x, y), z);
```
这样可以快速地比较多个数字的大小,而且代码简洁易懂。
c++判断三个string大小
可以使用 `>`、`<`、`==` 运算符对 `string` 类型进行比较,比较的结果是布尔类型的值,可以用 `if` 语句进行判断。具体实现代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
string str3 = "cpp";
if (str1 > str2 && str1 > str3) {
cout << "str1 is the largest" << endl;
} else if (str2 > str1 && str2 > str3) {
cout << "str2 is the largest" << endl;
} else if (str3 > str1 && str3 > str2) {
cout << "str3 is the largest" << endl;
} else {
cout << "The strings are equal" << endl;
}
return 0;
}
```
输出结果为:
```
str3 is the largest
```