原题链接:[编程入门]三个字符串的排序
解题思路:
注意事项:
参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <stdio.h> #include <math.h> #include <string.h> int main() { // 声明字符数组用于存储输入的三个字符串 char str1[100]; char str2[100]; char str3[100]; // 用于临时存储字符串,在排序时进行交换 char temp[100]; // 通过标准输入获取三个字符串 scanf( "%s" , str1); scanf( "%s" , str2); scanf( "%s" , str3); // 比较并排序字符串 // 第一轮比较和排序 if (strcmp(str1, str2) > 0) { // 如果 str1 大于 str2,则交换它们的内容,确保 str1 最小 strcpy(temp, str1); strcpy(str1, str2); strcpy(str2, temp); } if (strcmp(str1, str3) > 0) { // 如果 str1 大于 str3,则交换它们的内容,确保 str1 仍然最小 strcpy(temp, str1); strcpy(str1, str3); strcpy(str3, temp); } // 第二轮比较和排序 if (strcmp(str2, str3) > 0) { // 如果 str2 大于 str3,则交换它们的内容,确保 str2 比 str3 小 strcpy(temp, str2); strcpy(str2, str3); strcpy(str3, temp); } // 输出排序后的字符串 printf( "Sorted strings:\n" ); printf( "%s\n" , str1); // 输出最小的字符串 printf( "%s\n" , str2); printf( "%s" , str3); // 输出最大的字符串 return 0; } |
0 分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复