读入,输出优化

有时候用scanf,prinf会超时

怎么办呢?

这时候就要用到度如何输出优化了,下面看代码

读入优化

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;
}
读入优化

输出优化

void print(int x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
        print(x/10);
    putchar(x%10+'0');
}
输出优化

 

posted @ 2019-02-19 18:58  一个蒟蒻  阅读(160)  评论(1编辑  收藏  举报
titlechange
TOP