用C++编写输入三个正整数,从大到小输出
时间: 2023-03-28 20:04:19 浏览: 83
你好,我可以回答这个问题。以下是代码示例:
```
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个正整数:\n");
scanf("%d%d%d", &a, &b, &c);
if (a >= b && a >= c) {
if (b >= c) {
printf("%d %d %d\n", a, b, c);
} else {
printf("%d %d %d\n", a, c, b);
}
} else if (b >= a && b >= c) {
if (a >= c) {
printf("%d %d %d\n", b, a, c);
} else {
printf("%d %d %d\n", b, c, a);
}
} else {
if (a >= b) {
printf("%d %d %d\n", c, a, b);
} else {
printf("%d %d %d\n", c, b, a);
}
}
return ;
}
```
希望能对您有所帮助。
相关问题
用c++编写一个程序,将某个不确定位数的正整数进行三位分节后输出
以下是一个将正整数进行三位分节的C++程序:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string num_str;
cout << "请输入一个正整数:";
cin >> num_str;
int len = num_str.length();
int cnt = len % 3 == 0 ? len / 3 : len / 3 + 1;
cout << "分节后的结果为:";
for (int i = 0; i < cnt; i++) {
if (i == 0 && len % 3 != 0) {
cout << num_str.substr(0, len % 3);
if (cnt > 1) cout << ",";
}
else {
cout << num_str.substr(len % 3 + (i - 1) * 3, 3);
if (i < cnt - 1) cout << ",";
}
}
cout << endl;
return 0;
}
```
程序中使用了 `string` 类型来存储输入的正整数,通过 `length()` 函数得到字符串长度,从而确定分节的次数。在循环中,使用 `substr()` 函数来截取每个三位数,并且在末尾加上逗号(除了最后一个数)。注意需要特判第一个三位数的情况,因为可能不足三位数。
用c++编写一个程序,输出边长为n的直角三角形输入一个正整数n(n ≤ 1000) 输出一个n行的直角三角形
```
#include <stdio.h>
int main() {
int n;
printf("请输入一个正整数n:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
样例输入:
```
5
```
样例输出:
```
*
**
***
****
*****
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)