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

HDOJ 2564:词组缩写 标志位的用处很大呀

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

    这道题目的URL:http://acm.hdu.edu.cn/showproblem.php?pid=2564;

    写程序逻辑一定要清晰,有 清晰的逻辑才能写出正确的代码。要找出每个词的首字母,关键在于空格。第一个字母和一个或多个连续字母后的第一个字母便是下个单词的首字母。我用了一个标志位nextIsFirst来标志下一个是否是单词首字母。整个程序围绕这个标识位进行处理。

    下面的是我的AC代码,欢迎拍砖。

    

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

const int Max = 200;
char line[Max];
char acronym[20];

int main()
{
	int cases, pos, len;
	bool nextIsFirst;
	
	cin >> cases;
	getchar();
	while(cases--)
	{
		cin.getline(line, Max);
		pos = 0;
		nextIsFirst = true;
		len = strlen(line);
		for(int i=0; i<len; i++)
		{
			if(line[i] == ' ')
			{
				nextIsFirst = true;
			}
			else
			{
				if(nextIsFirst)
				{ 
					if(line[i] > 'Z')	acronym[pos++] = line[i] - 'a' + 'A';
					else acronym[pos++] = line[i];
					nextIsFirst = false;
				}
			}
		}
		acronym[pos] = '\0';
		printf("%s\n", acronym); 
	}
	system("pause");
	return 0;
}

抱歉!评论已关闭.