• ACM读入输出优化


    inline int read()  
    {  
        char ch;
    	bool flag = false;
        int a = 0;  
        while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));  
        if(ch != '-')
    	{
    		a *= 10;
    		a += ch - '0';  
    	}
    	else
    	{
    		flag = true;
    	}
        while(((ch = getchar()) >= '0') && (ch <= '9'))
    	{
    		a *= 10;
    		a += ch - '0';
    	}	
    	if(flag)
    	{
    		a = -a;
    	}
        return a;  
    }  
    void write(int a)  
    {  
    	if(a < 0)
    	{
    		putchar('-');
    		a = -a;
    	}
        if(a >= 10)
    	{
    		write(a / 10);
    	}		
        putchar(a % 10 + '0');  
    }  

    测试:

    数据生成

    #include <iostream>
    #include <cstdio>
    #include <windows.h>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    const int MAXN = 1000000;
    
    int main()
    {
    	freopen("in.txt", "w", stdout);
    	srand((unsigned)time(NULL));
    	for (int i = 1; i <= MAXN; i++)
    	{
    		printf("%d\n", rand());
    	}
    	return 0;
    }

    #include <iostream>
    #include <cstdio>
    #include <windows.h>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 1000000;
    
    inline int read()  
    {  
    	char ch;
    	bool flag = false;
    	int a = 0;  
    	while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));  
    	if(ch != '-')
    	{
    		a *= 10;
    		a += ch - '0';  
    	}
    	else
    	{
    		flag = true;
    	}
    	while(((ch = getchar()) >= '0') && (ch <= '9'))
    	{
    		a *= 10;
    		a += ch - '0';
    	}	
    	if(flag)
    	{
    		a = -a;
    	}
    	return a;  
    }  
    void write(int a)  
    {  
    	if(a < 0)
    	{
    		putchar('-');
    		a = -a;
    	}
    	if(a >= 10)
    	{
    		write(a / 10);
    	}		
    	putchar(a % 10 + '0');  
    }  
    
    void test1()
    {
    	int x;
    	DWORD start_time, end_time;
    	start_time = GetTickCount();
    	for (int i = 1; i <= MAXN; i++)
    	{
    		x = read();
    		//write(x);
    		//putchar('\n');
    	}
    	end_time = GetTickCount();
    	printf("%lf\n", (end_time - start_time) / 1000.0);
    }
    
    void test2()
    {
    	int x;
    	DWORD start_time, end_time;
    	start_time = GetTickCount();
    	for (int i = 1; i <= MAXN; i++)
    	{
    		scanf("%d", &x);
    		//printf("%d", x);
    		//putchar('\n');
    	}
    	end_time = GetTickCount();
    	printf("%lf\n", (end_time - start_time) / 1000.0);
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	test1();
    	test2();
    	return 0;
    }


    单纯进行读入操作:

    0.546000
    2.340000
    请按任意键继续. . .





  • 相关阅读:
    .emacs
    boost 程序库完全开发指南_ch3_memory_manager
    C++ 友元函数
    AutoHotKey.ini
    QQ因系统日期无法打开
    SharePoint 2007中如何更改用戶基本資料(EMail地址)
    盗版Windows XP如何安装IE7
    三人行,必有我師學會了家中布線ADSL上網
    以數據源方式讀取文本文件連接串及注事事項
    网络访问时you might not have permission to use this network resource错误解决方法
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835002.html
Copyright © 2020-2023  润新知