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

输入格式:

测试输入包含一个测试用例,在一行内给出总长度不超过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. Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp

    题目链接:http://codeforces.com/contest/597 A. Divisibility time limit per test 1 second memory limit per ...

  2. bzoj 1483: [HNOI2009]梦幻布丁 启发式合并vector

    1483: [HNOI2009]梦幻布丁 Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description N个 ...

  3. Linux环境下的定时任务(转载)

    今天做了个数据库的备份脚本,顺便系统得学习一下Linux下定时执行脚本的设置.Linux下的定时执行主要是使用crontab文件中加入定制计划来执行,设置比Windows稍微复杂一些(因为没有图形界面 ...

  4. python 分数的数学四则运算

    import fractions f1 = fractions.Fraction(, ) f2 = fractions.Fraction(, ) print('{} + {} = {}'.format ...

  5. 时间常用api

    1.常用api 创建 Date 对象  -  年  -  月  -  日   -  小时  -  分  -  秒 -  星期 var now=new Date() var year = now.get ...

  6. Flutter学习笔记(二)

    *.assets 当引用图片的时候,需要在pubspec.yaml的文件中的flutter下添加assets,类似于下面的样子: image.png 这里需要注意的是文件里的assets只要一个缩进即 ...

  7. 【Golang 接口自动化08】使用标准库httptest完成HTTP请求的Mock测试

    前言 Mock是一个做自动化测试永远绕不过去的话题.本文主要介绍使用标准库net/http/httptest完成HTTP请求的Mock的测试方法. 可能有的小伙伴不太了解mock在实际自动化测试过程中 ...

  8. Codeforces 101487E - Enter The Dragon

    101487E - Enter The Dragon 思路:做的时候两个地方理解错了,第一个事我以为龙吸了水,水就干了,其实龙是在下雨之前吸的,下雨时湖水又满了,所以湖水永远不会干:第二个是以为只要找 ...

  9. 枚举1--求小于n的最大素数

    枚举1--求小于n的最大素数 总结: 素数是不能被比它小的素数整除. /* 枚举就是基于已有知识镜像答案猜测的一种问题求解策略 问题:求小于n的最大素数 分析: 找不到一个数学公式,使得根据N就可以计 ...

  10. Linux下安装Phantomjs

    1. 安装linux系统的软件包 先来看一下官方网站的提示: Note: For this static build, the binary is self-contained. There is n ...