阅读435 返回首页    go 阿里云 go 技术社区[云栖]


poj 1750

题目很绕,看懂题目给出的测试用例不难,但是注意细节就不是那么简单的事情了,特别是这类字符串处理的题目。注意题目里面有一个隐藏的问题,如果是出现多个相同的单词怎么办?

应该是这样的效果:



下面出AC的代码:

#include<stdio.h>
#include<string.h>

char lastWord[100],nowWord[100];

int main()
{
	freopen("in.txt", "r", stdin);

	int spaceNum=0,SameNum=0;
	int i;

	scanf("%s",lastWord);
	printf("%s\n",lastWord);
	while(scanf("%s",nowWord)!=EOF)
	{
		for(i=0;i<strlen(lastWord) && i<strlen(nowWord);i++)
		{
			if(lastWord[i]!=nowWord[i])
				break;
		}

		//printf("i == %d\n",i);

		if (i>SameNum)
			spaceNum++;
		else
			spaceNum=i;

		SameNum=spaceNum;

		for(i=0;i<spaceNum;i++)
			putchar(' ');

		printf("%s\n",nowWord);
		strcpy(lastWord,nowWord);
	}

	return 0;
}


最后更新:2017-04-03 05:39:19

  上一篇:go Bison executable not found in PATH by mysql install
  下一篇:go C++ 虚函数表解析