• 【输入输出挂】【Uva11462】Age Sort


    例题17  年龄排序(Age Sort, UVa 11462)照从小到大的顺序输出。

    【输入格式】

    输入包含多组测试数据。每组数据的第一行为整数n(0<n≤2 000 000),即居民总数;下一行包含n个不小于1、不大于100的整数,即各居民的年龄。输入结束标志为n=0。

    输入文件约有25MB,而内存限制只有2MB。

    【输出格式】

    对于每组数据,按照从小到大的顺序输出各居民的年龄,相邻年龄用单个空格隔开。


    效率对比:




    输入输出挂

    inline int readint() {
      char c = getchar();
      while(!isdigit(c)) c = getchar();
    
      int x = 0;
      while(isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
      }    
      return x;
    }
    
    int buf[10]; 		//声明成全局变量可以减小开销
    inline void writeint(int i) {
      int p = 0;
      if(i == 0) p++; 	//特殊情况:i等于0的时候需要输出0,而不是什么也不输出
      else while(i) {
        buf[p++] = i % 10;
        i /= 10;
      }
      for(int j = p-1; j >=0; j--) putchar('0' + buf[j]); //逆序输出
    }
    
    1.注意3次getchar

    2.注意inline 

    3.注意i=0;

    4.注意buf的全局性



    完整代码:

    #include <cstdio>  
    #include <cstdlib>  
    #include <cmath>  
    #include <cstring>  
    #include <ctime>  
    #include <algorithm>  
    #include <cctype>
    #define uns unsigned  
    #define int64 long long  
    #ifdef WIN32  
    #define fmt64 "%I64d"  
    #else  
    #define fmt64 "%lld"  
    #endif  
    #define oo 0x13131313  
    using namespace std;  
    int n;
    int Sort[200];
    inline int readint()
    {
    	char c = getchar();
    	while(!isdigit(c)) c=getchar();
    	int x = 0;
    	while(isdigit(c))
    	{
    		x= x*10 +c - '0';
    		c=getchar();
    	}
    	return x;
    }
    int buf[10];
    inline void printfint(int i)
    {
    	int p=0;
    	if(i==0) p++;
    	else while(i)
    	{
    		buf[p++]=i%10;
    		i/=10;
    	}
    	for(int j=p-1;j>=0;j--) putchar('0'+buf[j]);
    }
    void output()
    {
    	int tot=0;
    	for(int i=1; i <= 100; i++)
    	 for(int j=1; j <= Sort[i]; j++)
    	  		{
    	  			printfint(i);
    				tot++;
    				if(tot!=n) printf(" ");
    			}
    	printf("
    ");
    }
    void input()
    {
    	int temp;
    	while(scanf("%d",&n)!=EOF&&n!=0)
    	{
    		memset(Sort,0,sizeof(Sort));
    		for(int i=1; i <= n ; i++ )
    		{
    			temp=readint();
    			Sort[temp]++;
    		}
    		output();
    	}
    }
    
    int main()
    {
    	input();
    	return 0;
    }


  • 相关阅读:
    重建索引报错-python数据分析
    机器学习实战1-2.1 KNN改进约会网站的配对效果 datingTestSet2.txt 下载方法
    机器学习实战1-1 KNN电影分类遇到的问题
    权限掩码umask详解
    spark配置(7)--on yarn配置
    spark配置(6)-独立应用程序
    spark配置(5)-独立应用程序
    spark配置(4)-----Spark Streaming
    spark配置(3)
    spark配置(2)
  • 原文地址:https://www.cnblogs.com/zy691357966/p/5480457.html
Copyright © 2020-2023  润新知