admin管理员组文章数量:1582665
本题就是测试读入数据的速度的。
如果有大量的数据读入,使用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/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;
}
测试过这个函数实在是太快了。
本文标签: 快速数据EnormousCodeChefInput
版权声明:本文标题:codechef Enormous Input Test 快速读入数据 fread 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1727901544a1136975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论