其他-读入优化与输出优化

朴素读入优化与输出优化

【a+b问题】:读入两个数,输出两数之和。

/*
    Coded by Ice_teapoy
*/
#include <cstdio>
#include <cctype>
//读入优化
int read()
{
    int res=0,f=1;
	char x=getchar();
    for (;!isdigit(x);x=getchar()) (x=='-')&&(f=-1);
    for (;isdigit(x);x=getchar()) res=(res<<3)+(res<<1)+(x^48);
    return f*res;
}
//输出优化
void write(int x)
{
    if (x<0) putchar('-'),x=-x;
    if (x>9) write(x/10);
    putchar(x%10+48);
}
int main()
{
	int a=read(),b=read();
	write(a+b);
}

fread、fwrite读入优化与输出优化

【a+b问题】:读入两个数,输出两数之和。
【输出10个数】:输出1到10十个数字,中间用回车隔开。

/*
    Coded by Ice_teapoy
*/
#include <cstdio>
#include <cctype>
//读入优化
inline char getcha()
{
    static char qwq[100000],*p1=qwq,*p2=qwq;
    return p1==p2&&(p2=(p1=qwq)+fread(qwq,1,100000,stdin),p1==p2)?EOF:*p1++;
}
int read()
{
    int res=0,f=1;
	char x=getcha();
    for (;!isdigit(x);x=getcha()) (x=='-')&&(f=-1);
    for (;isdigit(x);x=getcha()) res=(res<<3)+(res<<1)+(x^48);
    return f*res;
}
//输出优化
char pqwq[100000],*pp=pqwq;
void push(const char c)
{
    if (pp-pqwq==100000) fwrite(pqwq,1,100000,stdout),pp=pqwq;
    *pp++=c;
}
void write(int x)
{
    static int aa[35];
    int top=0;
    do aa[top++]=x%10,x/=10;
	while(x);
    while(top) push(aa[--top]+'0');
}
int main()
{
	//a+b问题 
	int a=read(),b=read();
	write(a+b);
	fwrite(pqwq,1,pp-pqwq,stdout);
	pp=pqwq;
	//一行一个输出1-10
	for (int i=1;i<=10;++i)
	{
		puts(""),write(i);
		fwrite(pqwq,1,pp-pqwq,stdout);
		pp=pqwq;
	}
}

【实数读入优化】

【a+b问题】:读入两个数,输出两数之和。

#include <cstdio>
#include <cctype>
inline double readb()
{
    double sum=0,Y=1.0;
    int f=1;
    char x=0;
    for (;!isdigit(x);x=getchar()) x=='-'?f=-1:1;
    for (;isdigit(x);x=getchar()) sum=sum*10+(x^48);
    x=getchar();
    for (;isdigit(x);x=getchar()) sum+=(Y/=10)*(x^48);
    return f*sum;
}
int main()
{
	double a=readb(),b=readb();
	printf("%.2lf",a+b);
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值