编程输入20位同学的姓名,按拼音顺序输出。
时间: 2023-06-16 15:05:54 浏览: 39
可以使用Python中的字典和sort()函数来实现。以下是示例代码:
```python
students = {
'张三': 'zhangsan',
'李四': 'lisi',
'王五': 'wangwu',
'赵六': 'zhaoliu',
'钱七': 'qianqi',
'孙八': 'sunba',
'周九': 'zhoujiu',
'吴十': 'wushi',
'郑十一': 'zhengshiyi',
'王十二': 'wangshier',
'冯十三': 'fengshisan',
'陈十四': 'chensishi',
'楚十五': 'chushiwu',
'魏十六': 'weishiliu',
'蒋十七': 'jiangshiqi',
'沈十八': 'shenshibai',
'韩十九': 'hanshijiu',
'杨二十': 'yangersh'
}
sorted_students = sorted(students.items(), key=lambda x: x[1]) # 按字典值(即拼音)排序
for student in sorted_students:
print(student[0])
```
以上代码中,我们将学生姓名与对应的拼音存储在一个字典中,然后使用sort()函数和lambda表达式来按拼音顺序排序字典,并最终输出排序结果。
相关问题
编程输入20位同学的姓名,按拼音顺序输出。,用C语言的方式做
可以使用C语言中的结构体和qsort函数来实现输入20位同学姓名并按拼音排序输出。
下面是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
struct Student {
char name[20];
};
// 比较函数,用于qsort排序
int cmp(const void *a, const void *b) {
return strcmp(((struct Student *)a)->name, ((struct Student *)b)->name);
}
int main() {
struct Student students[20];
int i;
// 输入学生姓名
for (i = 0; i < 20; i++) {
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", students[i].name);
}
// 按拼音排序
qsort(students, 20, sizeof(struct Student), cmp);
// 输出排序结果
printf("按拼音排序后的结果:\n");
for (i = 0; i < 20; i++) {
printf("%s\n", students[i].name);
}
return 0;
}
```
运行示例:
```
请输入第1个学生的姓名:张三
请输入第2个学生的姓名:李四
请输入第3个学生的姓名:王五
请输入第4个学生的姓名:赵六
请输入第5个学生的姓名:钱七
请输入第6个学生的姓名:孙八
请输入第7个学生的姓名:周九
请输入第8个学生的姓名:吴十
请输入第9个学生的姓名:郑一
请输入第10个学生的姓名:王二
请输入第11个学生的姓名:张三
请输入第12个学生的姓名:李四
请输入第13个学生的姓名:赵六
请输入第14个学生的姓名:孙八
请输入第15个学生的姓名:周九
请输入第16个学生的姓名:钱七
请输入第17个学生的姓名:吴十
请输入第18个学生的姓名:郑一
请输入第19个学生的姓名:王二
请输入第20个学生的姓名:张三
按拼音排序后的结果:
钱七
孙八
吴十
李四
李四
王二
王二
王五
张三
张三
张三
赵六
赵六
周九
周九
孙八
钱七
郑一
郑一
```
编程输入三个整数,按从小到大的顺序输出
可以使用以下代码实现:
```
a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
c = int(input("请输入第三个整数:"))
if a <= b and a <= c:
if b <= c:
print(a, b, c)
else:
print(a, c, b)
elif b <= a and b <= c:
if a <= c:
print(b, a, c)
else:
print(b, c, a)
else:
if a <= b:
print(c, a, b)
else:
print(c, b, a)
```
这段代码首先通过 `input()` 函数获取用户输入的三个整数,然后使用 `if` 语句进行比较,最终输出按从小到大排序后的结果。
相关推荐
![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)