
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;
} 展开
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;
} 展开
1个回答
展开全部
你现在的问题出在这里 cin>>c;
你需要在这句话后面加一句cin.ignore(1,'\n');
问题就是你出入c后回车了一下,那么输入缓冲区里面将保存这个回车符\n,第一次运行gets的时候,它得到了一个空串,就有内存问题了
当然了,改了这里之后,你后面程序的输出还是有点问题,但是问题是你的算法,而不是内存了
你需要在这句话后面加一句cin.ignore(1,'\n');
问题就是你出入c后回车了一下,那么输入缓冲区里面将保存这个回车符\n,第一次运行gets的时候,它得到了一个空串,就有内存问题了
当然了,改了这里之后,你后面程序的输出还是有点问题,但是问题是你的算法,而不是内存了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询