POJ 2471 解题报告

这道题是个简单题。但是我犯了个可能很常见的bug,然后用了很久才调试出来。我猜打印出中间过程,并验证是否符合预期,很快就能调出来。但我是通过测试数据+别人的正确程序中间结果(每段的size)对比出来的。。。

测试数据:

输入:http://www.informatik.uni-ulm.de/acm/Locals/2005/input/bingo.in

输出:http://www.informatik.uni-ulm.de/acm/Locals/2005/output/bingo.out

不用谢。

正确程序:http://www.cnblogs.com/lidaojian/archive/2012/04/19/2457448.html

后来发现错误的原因很简单。对每行处理完后,我没有考虑最后那个单词。于是for循环结束后,word里面有个未处理的单词。解决的办法是再判断是不是到行末了,如果是行末的话也处理下。

thestoryofsnow2471Accepted212K32MSC++1637B
/* 
ID: thestor1 
LANG: C++ 
TASK: poj2471 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

int gcd(int a, int b)
{
	if (b == 0)
	{
		return a;
	}
	return gcd(b, a % b);
}

int main()
{
	// printf("gcd(21, 18): %d\n", gcd(21, 18));
	// printf("gcd(12, 24): %d\n", gcd(12, 24));

	string line, word;
	set<string> words;
	int sum = 0;
	int ngames = 0;
	bool isChar = true;;
	while (getline(cin, line))
	{
		// printf("line: [%s]\n", line.c_str());
		for (int i = 0; i < line.size(); ++i)
		{
			isChar = true;
			if ('a' <= line[i] && line[i] <= 'z')
			{
				word.push_back(line[i]);
			}
			else if ('A' <= line[i] && line[i] <= 'Z')
			{
				word.push_back(line[i] - 'A' + 'a');
			}
			else
			{
				isChar = false;
			}

			if (!isChar || i == line.size() - 1)
			{
				if (word == "bullshit")
				{
					sum += words.size();
					// printf("ngames: %d, words.size(): %d\n", ngames, words.size());
					// cout << "words(" << words.size() << "): ";
					// for (set<string>::iterator iter = words.begin(); iter != words.end(); ++iter)
					// {
					// 	cout << "[" << *iter << "] ";
					// }
					// cout << endl;

					ngames++;
					words.clear();
				}
				else if (word.size() > 0)
				{
					words.insert(word);
				}

				word.clear();
			}
		}
	}

	// printf("sum: %d, ngames: %d\n", sum, ngames);
	int g = gcd(sum, ngames);
	printf("%d / %d\n", sum / g, ngames / g);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值