首页 > ACM题库 > HDU-杭电 > HDU 3456-Universal Oracle[解题报告]HOJ
2014
03-23

HDU 3456-Universal Oracle[解题报告]HOJ

Universal Oracle

问题描述 :

In computer science, an oracle is something that gives you the answer to a particular question. For this problem, you need to write an oracle that gives the answer to everything. But it’s not as bad as it sounds; you know that 42 is the answer to life, the universe, and everything.

输入:

The input consists of a single line of text with at most 1000 characters. This text will contain only well-formed English sentences. The only characters that will be found in the text are uppercase and lowercase letters, spaces, hyphens, apostrophes, commas, semicolons, periods, and question marks. Furthermore, each sentence begins with a single uppercase letter and ends with either a period or a question mark. Besides these locations, no other uppercase letters, periods, or question marks will appear in the sentence. Finally, every question (that is, a sentence that ends with a question mark) will begin with the phrase “What is…”

输出:

The input consists of a single line of text with at most 1000 characters. This text will contain only well-formed English sentences. The only characters that will be found in the text are uppercase and lowercase letters, spaces, hyphens, apostrophes, commas, semicolons, periods, and question marks. Furthermore, each sentence begins with a single uppercase letter and ends with either a period or a question mark. Besides these locations, no other uppercase letters, periods, or question marks will appear in the sentence. Finally, every question (that is, a sentence that ends with a question mark) will begin with the phrase “What is…”

样例输入:

Let me ask you two questions. What is the answer to life? What is the answer to the universe?

样例输出:

Forty-two is the answer to life.
Forty-two is the answer to the universe.

/*
	这是昨天比赛很水的题目,而我却没有做出来,真是不应该的;
	题目没有看仔细,What 以.结尾不要 回答的,这点注意就OK
	发现STL很强大,决定好好学下。
*/
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
	string str;
	while( getline(cin, str) )
	{
		string str1 = "What";
		size_t found1;
		found1 = str.find(str1);
		int i;
		while(found1 != string::npos)
		{
			int len = str.size();
			for(i = found1 + 4; i < len; i++)
			{
				if(str[i] == '?')
				{
					cout << "Forty-two";
					for(int j = found1 + 4; j < i; j++)
						cout << str[j];
					cout << "." << endl;
					//found1 = i;
					break;
				}
				if(str[i] == '.')
				{
					//found1 = i;
					break;
				}
			}
			found1 = str.find(str1, found1 + (i - 4 - found1));
		}
	}
}

参考:http://blog.csdn.net/huixisheng/article/details/5743845