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

输入格式:

测试输入包含一个测试用例,在一行内给出总长度不超过500 000的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用若干个空格分开。

输出格式:

每个测试用例的输出占一行,输出倒序后的句子,并且保证单词间只有1个空格。

输入样例:

Hello World   Here I Come

输出样例:

Come I Here World Hello
先放正确代码
 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; int main()
{
char s[];
gets(s);
int len=strlen(s),i,j;
bool flag=false;
for(i=len-;i>=;i--)
{
if(s[i]==' '&&s[i+]!=' ')
{
if(flag)
printf(" ");
for(j=i+;j<len;j++)
{
if(s[j]==' ')
break;
printf("%c",s[j]);
flag=true; }
}
else if(!i&&s[i+]!=' ')
{
if(flag)
printf(" ");
for(j=i;j<len;j++)
{
if(s[j]==' ')
break;
printf("%c",s[j]);
flag=true;
}
}
}
printf("\n");
return ;
}

再放错误的

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; int main()
{
char s[];
gets(s);
int len=strlen(s),i,j;
bool flag=false;
for(i=len-;i>=;i--)
{
if(s[i]==' '&&s[i+]!=' ')
{
if(!flag)
{
flag=true;
}
else
printf(" ");
for(j=i+;j<len;j++)
{
if(s[j]==' ')
break;
printf("%c",s[j]); }
}
else if(!i&&s[i+]!=' ')
{
if(!flag)
{
flag=true;
}
else
printf(" ");
for(j=i;j<len;j++)
{
if(s[j]==' ')
break;
printf("%c",s[j]); }
}
}
printf("\n");
return ;
}

错误就在于那个flag,导致格式错误,不过我不知道为什么会这样,希望哪位大神看到可以给我解答一下。

PAT  说反话-加强版   (20分)的更多相关文章

  1. PAT 数列求和-加强版   (20分)(简单模拟)

    给定某数字A(1≤A≤9)以及非负整数N(0≤N≤100000),求数列之和S=A+AA+AAA+⋯+AA⋯A(N个A).例如A=1, N=3时,S=1+11+111=123 输入格式: 输入数字A与 ...

  2. pat 1035 Password(20 分)

    1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the ...

  3. PAT 甲级 1035 Password (20 分)(简单题)

    1035 Password (20 分)   To prepare for PAT, the judge sometimes has to generate random passwords for ...

  4. PAT 1088 三人行(20 分)(暴力破解+流程分析)

    1088 三人行(20 分) 子曰:"三人行,必有我师焉.择其善者而从之,其不善者而改之." 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整数:把甲的能力值的 ...

  5. pat 1077 Kuchiguse(20 分) (字典树)

    1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  6. pat 1008 Elevator(20 分)

    1008 Elevator(20 分) The highest building in our city has only one elevator. A request list is made u ...

  7. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  8. PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)

    1061 Dating (20 分)   Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...

  9. PAT 1042 Shuffling Machine (20 分)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

随机推荐

  1. python 集合从头部删除元素

    num_set = , , , , ]) num_set.pop() print(num_set) num_set.pop() print(num_set)

  2. ros pbstream

    https://blog.csdn.net/xiekaikaibing/article/details/80320822

  3. webpack4试水总结

    看了官方的升级通告,据说webpack4的打包效率提升近一倍,于是最近在项目分支上升级了下webpack4,过程中的一些报错及问题简单整理下,以供交流. 在之前的旧项目上单纯的升级webpack版本后 ...

  4. 顶点与UV

    1.顶点坐标和UV坐标是三维模型重要的两个坐标系统. 2.什么是UV?UV分别是图像在显示器水平和垂直方向上坐标,值在 0 - 1 之间 ,即水平方向的第 U 个做像素/图片宽度,垂直方向的第 V 个 ...

  5. Python获取脚本所在目录的正确方法(转)

    1.以前的方法如果是要获得程序运行的当前目录所在位置,那么可以使用os模块的os.getcwd()函数.如果是要获得当前执行的脚本的所在目录位置,那么需要使用sys模块的sys.path[0]变量或者 ...

  6. 20170711xlVBA自定义分类汇总一例

    Public Sub CustomSubTotal() AppSettings On Error GoTo ErrHandler Dim StartTime, UsedTime As Variant ...

  7. TCP客户端与服务器的实现

    为了更容易理解,我们举一个小例子来说明服务器与客户端之间的连接过程. 有一个饭店,饭店里有服务员,服务员用于招待客人 特别要注意的是:要记住相关函数的各个参数都是什么,什么时候返回SOCKET_ERR ...

  8. Android studio jni

    首先我们要明确几个概念,jni,ndk,共享库(.so). jni是java native interface的缩写,java 本地接口.它提供了若干的API实现了Java和其他语言的通信(主要是C/ ...

  9. 贴心的vs 备注提醒功能

  10. OC MRC之循环引用问题(代码分析)

    // // main.m // 07-循环引用 // // Created by apple on 13-8-9. // Copyright (c) 2013年 itcast. All rights ...