美文网首页
【MAC 上学习 C++】Day 57-2. 实验8-2-8 字

【MAC 上学习 C++】Day 57-2. 实验8-2-8 字

作者: RaRasa | 来源:发表于2019-10-20 09:49 被阅读0次

实验8-2-8 字符串排序 (20 分)

1. 题目摘自

https://pintia.cn/problem-sets/13/problems/557

2. 题目内容

本题要求编写程序,读入5个字符串,按由小到大的顺序输出。

输入格式:

输入为由空格分隔的5个非空字符串,每个字符串不包括空格、制表符、换行符等空白字符,长度小于80。

输出格式:

按照以下格式输出排序后的结果:

After sorted:
每行一个字符串

输入样例:

red yellow blue green white

输出样例:

After sorted:
blue
green
red
white
yellow

3. 源码参考
#include <iostream>

using namespace std;

#define len 80

int main()
{
  char c[5][80], m[80];
  int i, j;

  for(i = 0; i < 5; i++)
  {
    cin >> c[i];
  }

  for(i = 0; i < 5; i++)
  {
    for(j = i + 1; j < 5; j++)
    {
      if(strcmp(c[i], c[j]) > 0)
      {
        strcpy(m, c[i]);
        strcpy(c[i], c[j]);
        strcpy(c[j], m);
      }
    }
  }

  cout << "After sorted:" << endl;
  for(i = 0; i < 5; i++)
  {
    cout << c[i] << endl;
  }
  
  return 0;
}

相关文章

网友评论

      本文标题:【MAC 上学习 C++】Day 57-2. 实验8-2-8 字

      本文链接:https://www.haomeiwen.com/subject/tlodmctx.html