codechef Enormous Input Test 快速读入数据 fread
2014-05-02 18:28
429 查看
本题就是测试读入数据的速度的。
如果有大量的数据读入,使用cin是很慢的。
那么使用scanf那么会快很多,但是如果数据量更大的话那么就还是不够快了。
所以这里使用fread。
首先开一个buffer,然后使用fread大块大块地读入数据就可以非常快地读入了。
题目如下:
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.
Write a single integer to output, denoting how many integers ti are divisible by k.
原题地址: http://www.codechef.com/problems/INTEST/
测试过这个函数实在是太快了。
如果有大量的数据读入,使用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; }
测试过这个函数实在是太快了。
相关文章推荐
- This is the first problem for test. Since all we know the ASCII code, your job is simple: Input numb
- 【CodeChef】Enormous Input Test
- 规模数据导入高效方式︱将数据快速读入R—readr和readxl包
- 将数据快速读入R—readr和readxl包
- sas优化技巧(4)执行必要的部分where,if、select,if else、obs firstobs、读入外部数据时选择需要的obs(if+input)、keep/drop
- spoj450 Enormous Input Test
- CodeChef Sereja and LCM(矩阵快速幂)
- 【CodeChef】Enormous Input Test
- 将数据快速读入R—readr和readxl包
- Android开发:使用AutoInputAuthCode快速实现自动填写验证码
- fread 快速读入
- 58.大数据读入内存快速排序并写入内存,创建排序好的文件
- 大数据文件快速读入内存
- raw_input如何设定读入的数据为数值类型
- 【程序48】 TestCode.java 题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下 //:每位数字 都加上5,然后用和除以10的余数代替该数字,再将
- codechef Factorial 算法 fread读入数据
- Numpy快速处理数据--ndarray对象(一)
- Mysql 大量数据快速导入导出
- Flex 3快速入门: 处理数据 使用 HTTPService 加载外部数据
- dotLucene在索引数据的时候似乎只能一次性全部读入数据后, 再进行索引.