输入若干个数(可能有整数和带小数的浮点数),升序排序后输出。输入格式: 在一行中输入若干个数,以空格分隔。 输出格式: 输出升序排序后的数,以空格分隔,最后没有空格
时间: 2023-05-31 20:04:10 浏览: 107
C++ 代码
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
double a[1005];
int n = 0;
while (cin >> a[n++]); // 输入数据
sort(a, a + n); // 排序
for (int i = 0; i < n - 1; i++) {
printf("%.3lf ", a[i]); // 输出结果,保留三位小数
}
printf("%.3lf", a[n - 1]); // 最后一个数后面无空格
return 0;
}
```
Python 代码
```python
a = list(map(float, input().split())) # 输入数据
a.sort() # 排序
for i in range(len(a) - 1):
print("%.3f" % a[i], end=" ") # 输出结果,保留三位小数
print("%.3f" % a[-1]) # 最后一个数后面无空格
```
相关问题
C++编程解决:输入一个整数和双精度浮点数,先将浮点数保留2位小数输出,然后输出整数。
好的,以下是C++代码实现:
```cpp
#include <iostream>
#include <iomanip> // 头文件,用于控制输出格式
using namespace std;
int main() {
int num1;
double num2;
cout << "请输入一个整数和一个双精度浮点数:" << endl;
cin >> num1 >> num2;
cout << fixed << setprecision(2) << num2 << endl; // 保留2位小数输出
cout << num1 << endl;
return 0;
}
```
程序会要求你输入一个整数和一个双精度浮点数,然后先输出浮点数并保留2位小数,再输出整数。注意要包含头文件iomanip,使用fixed和setprecision来控制输出格式。
C程序设计:输入一个整数,一个浮点数和一个字符,并输出
您好!这个问题是关于C程序设计的,我可以回答。您可以使用scanf函数输入整数、浮点数和字符,然后使用printf函数输出它们。具体的代码实现可以参考以下示例:
```c
#include <stdio.h>
int main()
{
int num;
float fnum;
char ch;
printf("请输入一个整数、一个浮点数和一个字符:\n");
scanf("%d %f %c", &num, &fnum, &ch);
printf("您输入的整数是:%d\n", num);
printf("您输入的浮点数是:%f\n", fnum);
printf("您输入的字符是:%c\n", ch);
return 0;
}
```
希望这个回答能够帮到您!
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)