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

输入格式:

测试输入包含一个测试用例,在一行内给出总长度不超过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. poj 1523 SPF 无向图求割点

    SPF Description Consider the two networks shown below. Assuming that data moves around these network ...

  2. c++ 满足条件拷贝,容器扩容(copy_if)

    #include <iostream> // cout #include <algorithm> // copy_if, distance #include <vecto ...

  3. HttpClient的用法总结

    使用HttpClient连接服务端的步骤: 1.创建HttpClient客户端对象 HttpClient client = new DefaultHttpClient(); 2.创建请求对象      ...

  4. Spring AMQP 源码分析 08 - XML 配置

    ### 准备 ## 目标 通过 XML 配置文件使用 Spring AMQP ## 前置知识 <Spring AMQP 源码分析 07 - MessageListenerAdapter> ...

  5. Codeforces 349B - Color the Fence

    349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...

  6. javascript对象使用总结

    javascript对象使用总结 一.总结 一句话总结:js对象的主要知识点是创建对象和继承,并且创建对象和继承的方法都是逐步层层递进的 创建对象 继承 原型 创建对象 1 <script> ...

  7. thinkphp5开发的网站出现”No input file specified”(php版本5.6.27)

    thinkphp5开发的网站出现”No input file specified”(php版本5.6.27) 一.总结 一句话总结:搜索引擎一定要用google,比百度节约时间一万倍,google啊, ...

  8. English trip -- VC(情景课)8 D Reading

    Listen and read. Shop Smart [smɑːt]  Employee of the Month: Sara['særə] (萨拉) Lopez(洛佩斯) Congratulati ...

  9. English trip -- VC(情景课)4 A Health

    Word doctor doctor's office medicine   [ˈmɛdɪsɪn]  n. 药:医学:内科:巫术  vt. 用药物治疗:给…用药 pill  n. 药丸 nurse   ...

  10. ccf画图

    问题描述 在一个定义了直角坐标系的纸上,画一个(x1,y1)到(x2,y2)的矩形指将横坐标范围从x1到x2,纵坐标范围从y1到y2之间的区域涂上颜色. 下图给出了一个画了两个矩形的例子.第一个矩形是 ...