閱讀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++ 虛函數表解析