zoukankan      html  css  js  c++  java
  • 小代码

    //
    //考虑到的BUG;
    //1: 输入指令不是-n或 -r
    //2: 输入了多次-n或-r
    //3:输入的指令参数个数大于2
    //4:-n或-r后面跟的不是数字或是负数
    //5:-n或-r后面可以跟带‘+’号的整数
    //
    //
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<ctype.h>
    #include<time.h>
    #define  N 2
    
    bool isNum(char* s);
    
    int main(int argc,char * argv[])
    {
        int ch;
        opterr=1;
        int count=-1;
        int scope=-1;
        int key=0;
        while((ch=getopt(argc,argv,"n:r:"))!=-1)
        {
             key++;
             if(!isNum(optarg))
             {
                 printf("非法参数
    ");
                 return -1;
             }
             switch(ch)
             {
                  case 'n':
                      count=atoi(optarg);
                      break;
                  case 'r':
                      scope=atoi(optarg);
                      break;
                  default:
                      printf("参数解析错误!
    ");
                      return -1;           
             }
        }
        if(key!=N||count<0||scope<=0)
        {
             printf("参数解析错误!
    ");
             return -1;
        }
        srand((int)time(NULL));
        for(int i=0;i<count;i++)
        {
             printf("%d
    ",rand()%scope);
        }
        return 0;
    }
    
    bool isNum(char* s)
    {
        if(s==NULL)  return false;
        char* p=s;
        if(*p=='+')  p++;
        bool res=true;
        while(*p!='')
        {
            if(!isdigit(*p))
            {
                 res=false;
            }
            p++;
        }
        return res;
    }
    

      

    #include<stdio.h>
    #include<stdlib.h>
    #include<pthread.h>
    #include<string.h>
    #include<unistd.h>
    #include<time.h> 
    
    #define GROUP 10
    
    int yy=10;
    int* tail[10];
    
    typedef struct no
    {
       void* base;
       size_t num;
       size_t width;
    }node;
    
    
    int comp(const void *a,const void* b)
    {
         return *(int*)a-*(int*)b;
    }
    
    
    
    void *myq(void *n)
    {
        node* no=(node*)(n);
        qsort(no->base,no->num,no->width,comp);
    }
    
    
    
    void adjust_heap(int* p[],int n,int &yy)
    {
    	int left=2*n+1;
    	int right=2*n+2;
    	if(n>=0&&n<yy/2)
    	{
        	int k=n;
    		if(left<yy&&*p[left]<*p[k])  k=left;
    	    if(right<yy&&*p[right]<*p[k])  k=right;
    	    if(k!=n)
    		{
    			int *temp=p[n];
    			p[n]=p[k];
    			p[k]=temp;
    			int* te=tail[n];
    			tail[n]=tail[k];
    			tail[k]=te;
    			adjust_heap(p,k,yy);
    		}	
    	}
    }
    
    
    void build_heap(int* p[])
    {
    	for(int i=yy/2-1;i>=0;i--)
    	{
        	adjust_heap(p,i,yy);
    	}
    }
    
    
    int main()
    {
        int count=0;
    	clock_t start,end,start1;
        start1 = clock();
        int* num=(int *)malloc(sizeof(int));
        int index=0;
        while(EOF!=scanf("%d",&num[index]))
        {
    		num=(int*)realloc(num,sizeof(int)*(index+2));
            getchar();
            index++;
            count++;
        }
        num=(int*)realloc(num,sizeof(int)*(index+1));
    	if(count<10)
    	{
    		qsort(num,count,sizeof(int),comp);
    		for(int i=0;i<count;i++)
    		{
    			printf("%d
    ",num[i]);
    		}
    
    		return 0;
    	}
        int k=count/GROUP;
        int tai=k;
        if(k*GROUP!=count)
        {
            tai=k+count-k*GROUP;
        }
        pthread_t t[10];
        for(int i=0;i<9;i++)
        {
            node *n=(node*)malloc(sizeof(node));
            n->base=num+(i*k);
            n->num=k;
            n->width=sizeof(int);
    
            pthread_create(&t[i],NULL,myq,n);
        }
        
        node n;
        n.base=num+9*k;
        n.num=tai;
        n.width=sizeof(int);
        pthread_create(&t[9],NULL,myq,&n);   
    
        for(int i=0;i<10;i++)
        {
            pthread_join(t[i],NULL);
        }
        
        start = clock();
        int* p[10];
    	for(int i=0;i<10;i++)
    	{
    		p[i]=num+(i*k);
    		tail[i]=&num[(i+1)*k];
    	}
        tail[9]=num+count;
    
        build_heap(p);
        
    	printf("
    ");
        for(int i=0;i<count;i++)
    	{
    		printf("%d
    ",*p[0]);
    		p[0]++;
            if(p[0]>=tail[0])  
    		{
    			int* temp=p[0];
    			p[0]=p[yy-1];
    			p[yy-1]=temp;
    
    			int* t=tail[0];
    			tail[0]=tail[yy-1];
    			tail[yy-1]=t;
    			yy--;
    		}
    		adjust_heap(p,0,yy);
    	}		
        
    	end = clock();
    
    	printf("%lf
    ",(double)(end - start)/CLOCKS_PER_SEC);
    
    	printf("%lf
    ",(double)(end - start1)/CLOCKS_PER_SEC);
        return 0;   
    }
    

      

  • 相关阅读:
    『中级篇』docker导学(一)
    计算机或许已经烂大街了,女生学计算机没出路吗?
    「初级篇」跟我一起学docker(17)--多节点mesos集群
    「初级篇」跟我一起学docker(18)--持续集成(初级终结篇)
    「初级篇」跟我一起学docker(16)--单节点mesos集群
    QT socket 多线程管理
    mysql数据库引擎 MyISAM和 InnoDB区别
    sql 删除表格delete drop truncate 区别(转)
    按层次遍历二叉树
    php基础排序算法 冒泡排序 选择排序 插入排序 归并排序 快速排序
  • 原文地址:https://www.cnblogs.com/sqxw/p/4185087.html
Copyright © 2011-2022 走看看