鱼C论坛

 找回密码
 立即注册
查看: 3555|回复: 5

[已解决]字符串

[复制链接]
发表于 2022-11-11 15:27:20 | 显示全部楼层 |阅读模式

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

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

x
输入三个字符串比较大小,并输出最小的字符串。求解答。
最佳答案
2022-11-11 16:00:31
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         char s[3][256] , s2[256] , s3[256]  , * p                                                                                            ;
  6.         for(int i = 0 ; i < 3 ; i ++) gets(s[i])                                                                                             ;
  7.         p = (strcmp(s[0] , s[1]) < 0 && strcmp(s[0] , s[2]) < 0) ? s[0] : (strcmp(s[1] , s[0]) < 0 && strcmp(s[1] , s[2]) < 0) ? s[1] : s[2] ;
  8.         printf("%s\n" , p)                                                                                                                   ;
  9. }
复制代码

        编译运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. ABCDE
  4. 12345678
  5. efghij
  6. 12345678

  7. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-11 15:50:29 | 显示全部楼层
  1. #include <cstdio>
  2. #include <cstring>

  3. char a[101];
  4. char b[101];
  5. char c[101];
  6. int main()
  7. {
  8.     scanf("%s",a);
  9.     scanf("%s",b);
  10.     scanf("%s",c);

  11.     if(strcmp(a,b)<0)
  12.     {
  13.         if(strcmp(a,c)<0)
  14.         {
  15.             printf("%s",a);
  16.         }
  17.         else
  18.         {
  19.             printf("%s",c);
  20.         }
  21.     }
  22.     else
  23.     {
  24.         if(strcmp(b,c)<0)
  25.         {
  26.             printf("%s",b);
  27.         }
  28.         else
  29.         {
  30.             printf("%s",c);
  31.         }
  32.     }

  33.     return 0;
  34. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-11 15:54:47 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>

  3. using std::cin, std::cout, std::endl;
  4. using std::string;

  5. int main(void) {
  6.     string a, b, c;
  7.     cin >> a >> b >> c;
  8.     cout << (a < b ? a < c ? a : c : b < c ? b : c) << endl;
  9.     return 0;
  10. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-11 16:00:31 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         char s[3][256] , s2[256] , s3[256]  , * p                                                                                            ;
  6.         for(int i = 0 ; i < 3 ; i ++) gets(s[i])                                                                                             ;
  7.         p = (strcmp(s[0] , s[1]) < 0 && strcmp(s[0] , s[2]) < 0) ? s[0] : (strcmp(s[1] , s[0]) < 0 && strcmp(s[1] , s[2]) < 0) ? s[1] : s[2] ;
  8.         printf("%s\n" , p)                                                                                                                   ;
  9. }
复制代码

        编译运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. ABCDE
  4. 12345678
  5. efghij
  6. 12345678

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

使用道具 举报

发表于 2022-11-11 16:02:31 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAXN 105
  4. char a[MAXN], b[MAXN], c[MAXN];

  5. int main()
  6. {
  7.         scanf("%s", a);
  8.         scanf("%s", b);
  9.         scanf("%s", c);
  10.        
  11.         if(strcmp(a, b) <= 0 && strcmp(a, c) <= 0)      printf("%s", a);
  12.         else if(strcmp(b, a) <= 0 && strcmp(b, c) <= 0) printf("%s", b);
  13.         else                                            printf("%s", c);
  14.         return 0;
  15. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-12 01:23:02 | 显示全部楼层
#include <iostream>
#include <string>
using namespace std;

int main(void) {
     string a, b, c;
     cin >> a >> b >> c;
     if (a < b)
     {
         if (a < c)
         {
             cout << a << endl;
         }
         else
         {
             cout << c << endl;
         }
     }
     else
     {
         if (b < c)
         {
             cout << b << endl;
         }
         else
         {
             cout << c << endl;
         }
     }
    return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 08:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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