c++简单题,跪求高手解答指错

Ignatiuslikestowritewordsinreverseway.GivenasinglelineoftextwhichiswrittenbyIgnatius,... Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.

Output
For each test case, you should output the text which is processed.

Sample Input

3
olleh !dlrow
m'I morf .udh
I ekil .mca

Sample Output

hello world!
I'm from hdu.
I like acm.

Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int i,t,c,x,j;
char a[1001];
cin>>c;
while(c--)
{
gets(a);
for(i=0;;i++)
if(a[i]==' ')
{
x=i;
break;
}
for(i=x-1;i>=0;i--)
cout<<a[i];
cout<<' ';
for(i=x;a[i]!='\0';i++)
{
if(a[i]==' '&&i!=x)
{
t=i;
for(j=t-1;a[j]!=' ';j--)
cout<<a[j];
}
}
cout<<endl;
}
return 0;
}
展开
 我来答
无滴阿1B
2009-09-23 · TA获得超过448个赞
知道小有建树答主
回答量:95
采纳率:0%
帮助的人:176万
展开全部
你现在的问题出在这里 cin>>c;
你需要在这句话后面加一句cin.ignore(1,'\n');
问题就是你出入c后回车了一下,那么输入缓冲区里面将保存这个回车符\n,第一次运行gets的时候,它得到了一个空串,就有内存问题了
当然了,改了这里之后,你后面程序的输出还是有点问题,但是问题是你的算法,而不是内存了
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式