您的位置:首页 > 其它

codechef Enormous Input Test 快速读入数据 fread

2014-05-02 18:28 429 查看
本题就是测试读入数据的速度的。

如果有大量的数据读入,使用cin是很慢的。

那么使用scanf那么会快很多,但是如果数据量更大的话那么就还是不够快了。

所以这里使用fread。

首先开一个buffer,然后使用fread大块大块地读入数据就可以非常快地读入了。

题目如下:


Input

The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 109, each.


Output

Write a single integer to output, denoting how many integers ti are divisible by k.


Example

Input:
7 3
1
51
966369
7
9
999996
11

Output:
4

原题地址: http://www.codechef.com/problems/INTEST/
#include <stdio.h>
namespace{
#define SIZE 65536
}

int EnormousInputTest()
{
	char buffer[SIZE];
	unsigned n, k, c;
	scanf("%u%u\n", &n, &k);
	unsigned ans = 0;
	int num = 0;
	while ((c = fread(buffer, 1, SIZE, stdin)) > 0)
	{
		for (unsigned i = 0; i < c; i++)
		{
			if (buffer[i] == '\n')
			{
				if (num % k == 0) ans++;
				num = 0;
			}
			else
			{
				num = num * 10 + buffer[i] - '0';
			}
		}
	}
	printf("%u", ans);
	return 0;
}


测试过这个函数实在是太快了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: