Problem
Given a list of space separated words, reverse the order of the words. Each line of text contains L
letters and W
words.
A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.
Input
The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line.
Output
For each test case, output one line containing "Case #x: " followed by the list of words in reverse order.
Limits
Small dataset
N = 5
1 ≤ L ≤ 25
Large dataset
N = 100
1 ≤ L ≤ 1000
Sample
看输入数据就知道是把单词颠倒过来。想起以前有种getline的字符串操作,用起来不错,就是记性不好,记不住怎么用了。两份代码。。。
#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;
char a[1010];
int main()
{
int t;
freopen("B-large-practice.in","r",stdin);
freopen("output.out","w",stdout);
scanf("%d",&t);
getchar();
int h=0;
while (t--)
{
int j;
char s[1010];
gets(s);
int l=strlen(s)-1;
printf("Case #%d: ",++h);
while (l>=0)
{
int i=0;
while (s[l]!=' ' && l>=0)
{
a[i++]=s[l];
l--;
}
for (j=i-1;j>=0;j--)
cout<<a[j];
if (l==-1)
{
printf("\n");
break;
}
else printf(" ");
l--;
}
}
return 0;
}
#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;
char a[1010][1010];
int main()
{
int t;
freopen("B-large-practice.in","r",stdin);
freopen("output.out","w",stdout);
scanf("%d",&t);
getchar();
int h=0;
while (t--)
{
string line,ss;
while (getline(cin,line))
{
istringstream s(line);
int i=0,j;
while (s>>a[i++]);
printf("Case #%d: ",++h);
for (j=i-2;j>0;j--)
cout<<a[j]<<" ";
cout<<a[0]<<endl;
}
}
return 0;
}