码迷,mamicode.com
首页 > 其他好文 > 详细

常用快速读入快速输出

时间:2017-07-05 22:55:59      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:put   include   scanf   ica   速度   ret   代码   运行速度   操作   

此文为博主原创,转载时请通知博主,并把原文链接放在正文醒目位置。

简要介绍

众所周知,就运行速度而言,getchar()快于scanf快于cin,putchar()快于printf快于cout.

但getchar()和putchar()每次只能操作一个字符,使用起来比较麻烦。

于是就出现了快读、快速输出。

它们的原理都是把需要读入\输出的数字(本文只能用于处理整数)一位一位地输出,从而减少运行时间。

 

代码

 1 #include<cstdio>
 2 using namespace std;
 3 
 4 inline void read(int &x)
 5 {
 6     char ch = getchar();
 7     char c;
 8     x = 0;
 9     while(ch > 9 || ch < 0)    
10         c = ch,ch = getchar();
11     while(ch <= 9 && ch >= 0)    
12         x = x*10 + ch-0,ch = getchar();
13     if(c == -)    x = ~x + 1;
14 }
15 
16 inline void put(int x)
17 {//快速输出 
18     if (x < 0)
19         x = ~x + 1, putchar(-);    
20     if (x > 9) 
21         put(x / 10);putchar(x % 10 + 0);
22 }
23     
24 int main()
25 {
26     int n;
27     read(n);
28     put(n);
29     return 0;
30 }

在实际应用时,可以把‘0‘写成48,‘9‘写成57,继续缩小所需时间。

 

常用快速读入快速输出

标签:put   include   scanf   ica   速度   ret   代码   运行速度   操作   

原文地址:http://www.cnblogs.com/shingen/p/7123653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!