TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/************************************************************************/
- /*
- 15. 有 N个国家名,要求按字母先后顺序排列(用起泡排序法)后输出
-
- */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
-
- void swap15(char *c1,char *c2)
- {
- char tempc[20];
- strcpy_s(tempc,c1);
- strcpy_s(c1,sizeof(tempc), c2);
- strcpy_s(c2,sizeof(tempc), tempc);
- }
- void main()
- {
- const int N=10;
- char Cun [N][20]; // 两层数组表示国名
- for (int i=0;i<N;i++)
- {
- gets_s(Cun); // 从键盘获取国名
- }
- for (int i=0;i<N-1;i++) // 冒泡排序法
- {
- for (int j=1;j<N-i;j++)
- {
- if (strcmp( Cun[j],Cun[j-1])<0) // 使用strcmp来比较字符串
- {
- swap15(Cun[j],Cun[j-1]); // 条件成立则交换
- }
- }
- }
- for (int i=0;i<N;i++) // 打印新的国名顺序
- {
- puts(Cun);
- }
- system("pause");
- }</pre><br><br><br></pre>
|
|