现在的位置: 首页 > 综合 > 正文

HDOJ 1062:Text Reverse 单词翻转

2018年05月25日 ⁄ 综合 ⁄ 共 550字 ⁄ 字号 评论关闭

      URL:http://acm.hdu.edu.cn/showproblem.php?pid=1062

      我的处理思路是:遇到空格或’\0’之后便将已经出现的单词反转。重要的是'\0'要考虑进去。

      这是AC源代码:

     

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;

const int Max = 1000 + 10;
char str[Max];
char w[Max];

int main()
{
	int cases;
	cin >> cases;
	char c = getchar();
	while(cases--)
	{
		gets(str);
		int pos = 0;
		int len = strlen(str);
		for(int i=0; i<=len; i++)
		{
			if(str[i] == ' ')
			{
				w[pos++] = '\0';
				strrev(w);
				printf("%s ", w);
				pos = 0;
			}
			else if(str[i] == '\0')
			{
				w[pos++] = '\0';
				strrev(w);
				printf("%s", w);
				pos = 0;
			}
			else
			{
				w[pos++] = str[i];
			}
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

抱歉!评论已关闭.