鱼C论坛

 找回密码
 立即注册
查看: 937|回复: 1

[已解决]数组问题

[复制链接]
发表于 2021-11-28 12:18:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
有点搞不懂这道题目怎么做
最佳答案
2021-11-28 12:38:42
本帖最后由 jackz007 于 2021-11-28 12:42 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         char s[5][80]                                      ;
  6.         int d[5] = {0 , 1 , 2 , 3 , 4}                     ;
  7.         int i , j , t                                      ;
  8.         for(i = 0 ; i < 5 ; i ++) scanf("%s" , s[i])       ;
  9.         for(i = 0 ; i < 4 ; i ++) {
  10.                 for(j = i + 1 ; j && strcmp(s[d[j]] , s[d[j - 1]]) < 0 ; j --) {
  11.                         t = d[j - 1]                       ;
  12.                         d[j - 1] = d[j]                    ;
  13.                         d[j] = t                           ;
  14.                 }
  15.         }
  16.         printf("%s" , s[d[0]])                             ;
  17.         for(i = 1 ; i < 5 ; i ++) printf("\t%s" , s[d[i]]) ;
  18.         printf("\n")                                       ;
  19. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 北京 上海 青岛 厦门 哈尔滨
  4. 北京    哈尔滨  青岛    上海    厦门

  5. D:\00.Excise\C>x
  6. beijing shanghai qindao xiamen haerbin
  7. beijing haerbin qindao  shanghai        xiamen

  8. D:\00.Excise\C>
复制代码
W3$IG1_XT)OXJ@9Y6$_B{4R.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-28 12:38:42 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-11-28 12:42 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         char s[5][80]                                      ;
  6.         int d[5] = {0 , 1 , 2 , 3 , 4}                     ;
  7.         int i , j , t                                      ;
  8.         for(i = 0 ; i < 5 ; i ++) scanf("%s" , s[i])       ;
  9.         for(i = 0 ; i < 4 ; i ++) {
  10.                 for(j = i + 1 ; j && strcmp(s[d[j]] , s[d[j - 1]]) < 0 ; j --) {
  11.                         t = d[j - 1]                       ;
  12.                         d[j - 1] = d[j]                    ;
  13.                         d[j] = t                           ;
  14.                 }
  15.         }
  16.         printf("%s" , s[d[0]])                             ;
  17.         for(i = 1 ; i < 5 ; i ++) printf("\t%s" , s[d[i]]) ;
  18.         printf("\n")                                       ;
  19. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 北京 上海 青岛 厦门 哈尔滨
  4. 北京    哈尔滨  青岛    上海    厦门

  5. D:\00.Excise\C>x
  6. beijing shanghai qindao xiamen haerbin
  7. beijing haerbin qindao  shanghai        xiamen

  8. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 13:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表