PAT 1009 说反话

https://pintia.cn/problem-sets/994805260223102976/problems/994805314941992960

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。

输出格式:每个测试用例的输出占一行,输出倒序后的句子。

输入样例:

Hello World Here I Come

输出样例:

Come I Here World Hello

代码:
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
char s[maxn];
char a[100][100];

int main()
{
  int n=0;
  cin.getline(s, 100);
  int len = strlen(s);
  s[len]=' ';
  len++;
  s[len]='\0';
  for(int i=0; i<len; i++)
  {
    if(s[i]!=' ')
      for(int j=i; j<len; j++)
      {
        if(s[j]!=' ')
          continue;
        else
        {
          n++;
          for(int zzt=i; zzt<j; zzt++)
            a[n][zzt-i]=s[zzt];
        }
        i=j;
        break;
      }
  }
  for(int i=n; i>=1; i--)
  {
    if(i!=1)
      printf("%s ",a[i]);
    else
      printf("%s\n",a[1]);
  }
  return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/zlrrrr/p/9260674.html