在C/C ++中,对于int的输入速度比scanf快 [英] Input faster than scanf for int in c/c++

查看:132
本文介绍了在C/C ++中,对于int的输入速度比scanf快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在解决一个编码竞赛中的一个问题,在这个竞赛中,我必须付出很大的投入.

Hello everyone, I am solving a problem in a coding competition in which i have to give very large input.

正如问题所指出的-输入文件的大小可能达到近8MB,因此请确保您使用的是快速I/O方法.

As stated int the problem - The input file size could reach almost 8MB so be sure you are using fast I/O method.

输入将以测试用例的数量给出. 然后是正方形矩阵的尺寸n. 然后整个矩阵为

The input is to be given as number of test cases. Then the dimension n of the square matrix. and then the whole matrix as

2

3

2 6 4

4 8 9

7 9 4

2

8 4

15 4

我从网上某处获得了一个代码,但我认为它没有任何帮助,因为与此同时,我也收到了消息时间限制已超出". 请告诉我们以下代码是否可以快速输入

I have got a code from somewhere on net but i don't think that it is doing any help because with this also i am getting message Time Limit Exceeded. Please tell that if the following code will do fast input or not

#define BUF 406 // block size on my disk is 4KBs
char ibuf[BUF];
int ipt = BUF;
int read_uint() {
while (ipt < BUF && ibuf[ipt] < '0') ipt++;
if (ipt == BUF) 
{
    fread(ibuf, 1, BUF, stdin);
    ipt = 0;
    while (ipt < BUF && ibuf[ipt] < '0') ipt++;
}
int n = 0;
while (ipt < BUF && ibuf[ipt] >= '0') n = (n*10)+(ibuf[ipt++]-'0');
if (ipt == BUF) 
{
    fread(ibuf, 1, BUF, stdin);
    ipt = 0;
    while (ipt < BUF && ibuf[ipt] >= '0') n = (n*10)+(ibuf[ipt++]-'0');
}
return n;
}

在这种情况下,请告诉我们读取int的最快方法

And please tell the fastest method of reading input for int in this case

非常感谢您

推荐答案

您只是为了读取输入内容而获得TLE吗?通过提交仅读取数据的代码,确保数据读取操作需要花费多少时间.以下gets基本代码大约需要 0.50s 来读取大约 10MB 数据文件,而scanf版本需要大约 1s

Are you getting TLE just for reading the inputs? Make sure how much time data-reading operation takes by submitting a code which only reads the data. The following gets base code take about 0.50s to read about 10MB data file while scanf version take about 1s

使用gets方法读取输入:

Read input using gets method:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

int matrix[100][100];

int main () {
    char arr[1200], *p;
    int n, j;
    clock_t tStart = clock();
    gets(arr);
    n = atoi(arr);
    for (; n--;) {        
        gets(arr);
        int siz = atoi(arr);
        for (int i=0; i<siz; i++) {
            gets(arr);
            p = strtok(arr, " ");
            j=0;
            while(p != NULL) {
                matrix[i][j++] = atoi(p);
                p = strtok(NULL, " ");
            }
        }
    }

    printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}

scanf版本:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

int matrix[100][100];

int main () {
    int n, j;
    clock_t tStart = clock();
    scanf("%d", &n);
    for (; n--;) { 
        int siz;
        scanf("%d", &siz);
        for (int i=0; i<siz; i++) {
            j=0;
            while(j<siz) {
                scanf("%d", &matrix[i][j++]);
            }
        }
    }

    printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}

这篇关于在C/C ++中,对于int的输入速度比scanf快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆