codechef Enormous Input Test 快速读入数据 fread

本题就是测试读入数据的速度的。


如果有大量的数据读入,使用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;
}

测试过这个函数实在是太快了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值