读入优化和输出优化

前两天做牛客的题  才知道有这么个玩意。。。。。

果然还是太菜  大佬们打比赛是去切题  我是去认识新名词。。。。。。

题目也很直白  说了 

读入文件较大,请使用读入优化,本机调试时请使用文件输入输出

而且题目  也给出了优化写法 

可以直接用  非常良心

inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
用法:
int a = read(), b = read();
cout << a + b;

自己到网上看了看    大佬的讲解博客传送门

这个东西  还是有一点用的

根据大佬的测试   每50万组数据读入优化要比scanf快0.1秒(100ms)

那么一百万组数据 就快了0.2秒了  相当不错

下面给出代码模板

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
void read(int &x){
    int f=1;//标记正负号
    x=0;//初始化
    char s=getchar();
    while(s<'0'||s>'9'){//读入符号
        if(s=='-') f=-1;
        s=getchar();
    }
    while(s>='0'&&s<='9'){//读入数字
        x=x*10+s-'0';
        s=getchar();
    }
    x*=f;
    return ;
}
void print(int x){
    if(x<0){
        putchar('-');x=-x;
    }
    if(x>9)  print(x/10);//这里是个递归  所以虽然是按照从数字末尾到开头的顺序处理
    putchar(x%10+'0');      //但是输出的时候会倒着输出 也就是正序
    return ;
}
int main(){
    int x;
    while(1){
        read(x);
        print(x);
        printf("\n");
    }
    return 0;
}

A+B问题

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int read(){
    int out=0,fh=1;
    char cc=getchar();
    if (cc=='-') fh=-1;
    while (cc>'9'||cc<'0') cc=getchar();
    while (cc>='0'&&cc<='9') {out=out*10+cc-'0';cc=getchar();}
    return out*fh;
}
void write(int x)  
{  
    if (x==0){
        putchar('0');
        return;
    }
    int num = 0; char c[15];
    while(x) c[++num] = (x%10)+48, x /= 10;
    while(num) putchar(c[num--]);
    putchar(' '); 
}
int main(){
    write(read()+read());
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1900_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值