输入10个实数套入数组(float n[18];)中,按从小到大顺序排序后输出。 输出格式如样张所示。要求:输入数据使用“.%f”格式。
时间: 2023-06-18 12:06:37 浏览: 74
以下是一个C语言的示例代码,可以实现你的要求:
```c
#include <stdio.h>
int main() {
float n[10];
int i, j;
float temp;
// 输入10个实数
printf("请输入10个实数:\n");
for (i = 0; i < 10; i++) {
scanf("%f", &n[i]);
}
// 冒泡排序
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (n[j] > n[j + 1]) {
temp = n[j];
n[j] = n[j + 1];
n[j + 1] = temp;
}
}
}
// 输出排序后的结果
printf("从小到大排序后的结果:\n");
for (i = 0; i < 10; i++) {
printf("%.2f ", n[i]);
}
return 0;
}
```
上述代码中,我们使用了冒泡排序算法来对输入的10个实数进行排序。在排序完成后,我们按照“%.2f”格式输出排序后的结果。你可以根据自己的需要调整输出的格式。
相关问题
10个float型实数存放在一个数组中,从小到大排序后输出。
可以,以下是示例代码:
```python
arr = [3.2, 1.1, 5.5, 2.0, 7.3, 6.8, 4.4, 9.9, 8.5, 0.7]
arr.sort() # 排序
for num in arr:
print(num) # 输出
```
输出结果为:
```
0.7
1.1
2.0
3.2
4.4
5.5
6.8
7.3
8.5
9.9
```
c语言编程输入三个实数要求按从小到大的顺序输出
你可以使用以下的C语言代码实现输入三个实数,并按照从小到大的顺序输出:
```c
#include <stdio.h>
int main()
{
float a, b, c;
printf("请输入三个实数:\n");
scanf("%f %f %f", &a, &b, &c);
if (a > b) {
float temp = a;
a = b;
b = temp;
}
if (b > c) {
float temp = b;
b = c;
c = temp;
}
if (a > b) {
float temp = a;
a = b;
b = temp;
}
printf("从小到大的顺序输出:%f %f %f\n", a, b, c);
return 0;
}
```
上述代码中,我们使用了三个if语句和一个临时变量temp来确定三个实数的大小关系,从而实现了按照从小到大的顺序输出。
相关推荐
![](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)