4114 单词逆序(nenuOJ)

Problem Description

对于每个单词,在不改变这些单词之间的顺序下,逆序输出每个单词。
这个问题包括多组测试数据。第1行是一个整数N,紧接着是一个空行,然后是N组测试数据,每组数据之间有一个空行。要求输出N个输出块。每个输出块之间有一个空行。

Input

第1行为一个整数,表示测试数据的组数。每个测试数据占一行,包含若干个单词,单词之间用一个空格隔开,每个单词仅由大小写字母字符组成。

Output

对每组数据中的每个测试数据,输出一行。

Sample Input
1

3
I am happy today
To be or not to be
I want to win the practice contest

Sample Output
I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc

因为还是菜鸟,所以请教了学长,代码如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
char s[maxn];
stack<char> st;
void solve() {
	int len = strlen(s);
	for(int i = 0; i < len; ++i) {
		if(s[i] == ' ') {
			while(!st.empty()) {
				putchar(st.top());
				st.pop();
			}
			printf(" ");	
		} else if(isalpha(s[i])) {
			st.push(s[i]);
		}
	}
	while(!st.empty()) {
		putchar(st.top());
		st.pop();
	}
	puts("");
}
int main() {
	int T; 
	while(scanf("%d", &T)!=EOF){
		for(int cs = 1; cs <= T; ++cs) {
		int n; scanf("%d", &n); 
		getchar();
		
		for(int i=0;i<n;i++){
			fgets(s, maxn, stdin);
			solve();
		} 
		
	//	puts("");
	}
	}
	
	return 0;
}

整体思路是对的,应该是格式空行上有点问题,如果有人知道麻烦告诉我一下。

发布了4 篇原创文章 · 获赞 0 · 访问量 67

猜你喜欢

转载自blog.csdn.net/qq_44123044/article/details/104332732