HDU - 1062

题目来源HDU - 1062
总感觉自己的思路新奇。。。

问题描述

XUPT_ACM的泰泰学长智商180,他有个怪癖——写字的时候喜欢倒着写。泰泰学长的女朋友收到泰泰学长的信可发了愁,你能帮泰泰学长的女朋友翻译吗?

INPUT

输入包含多组数据。 第一行输入一个正整数 T 表示总的行数。接下来是T行。

每行包含一些单词,每行最多1000个字符。

OUTPUT

对于每一行输入,请你都把他倒过来,泰泰学长的女朋友会谢谢你的哟。

Sample Input

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

Sample Output

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

#include<iostream>
#include<cstring>
const int Max = 100005;
char s[Max];
using namespace std;
int main()
{
	int t;
	cin >> t;
	getchar();
	while (t--) {
		gets(s);
		int flag,i;
		flag = 0;
		for (i = 0;i < strlen(s);i++) {
			if(s[i] == ' ') {
				for (int j = (i - 1);j >= flag;j--) {
					cout << s[j];
				}
				cout << s[i];
				flag = i+1;
			} 
			else if(i == strlen(s) - 1) {
				for (int j = i;j >= flag;j--) {
					cout << s[j];
				}
			}
		}
		cout << endl;
	}
}
用stl里的stack的先进后出来做
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
const int Max = 1000005;
char str[Max];
stack<char> s;
int main()
{
	int t;
	cin >> t;
	getchar();
	while (t--) {
		gets(str); 
		for (int i = 0;i < strlen(str);i++){
			if (str[i] == ' ') {
				while(!s.empty()) {
					cout << s.top();
					s.pop();
				}
				cout << ' ';
			}
			else 
				s.push(str[i]);
		}
		while(!s.empty()) {
					cout << s.top();
					s.pop();
				}
				cout << endl;
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值