zoukankan      html  css  js  c++  java
  • zjut 1204 01串排序

    01串排序  Time Limit:1000MS  Memory Limit:32768K

    Description:

    将01串首先按长度排序,长度相同时,按1的个数多少进行排序,1的个数相同时再按ASCII码值排序。

    Input:

    输入数据中含有一些01串,01串的长度不大于256个字符。

    Output:

    重新排列01串的顺序。使得串按基本描述的方式排序。

    Sample Input:

    10011111
    00001101
    1010101
    1
    0
    1100
    

    Sample Output:

    0
    1
    1100
    1010101
    00001101
    10011111




    尝试1
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char s[10000][256];
    
    int cmp ( const void *a , const void *b )
    {   
     return strcmp((char*)a,(char*)b);
    }
    
    
    int main(int argc, char *argv[])
    {   int i,n;
     n=0;
     while ( scanf("%s",s[n])!=EOF && s[n][0]!='#' ) n++; 
      qsort(s,n,sizeof(s[0]),cmp);
        for (i=0; i<n; i++)
         printf("%s
    ",s[i]);     
     return 0;
    }
    View Code

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    char s[10000][256];

    int cmp ( const void *a , const void *b )

             {         return strcmp((char*)a,(char*)b);      }

    int main(int argc, char *argv[])

    {  

               int i,n;  n=0;

     while ( scanf("%s",s[n])!=EOF && s[n][0]!='#' )   n++;

           qsort(s,n,sizeof(s[0]),cmp);    

    for (i=0; i<n; i++)   

         printf("%s ",s[i]);   

       return 0;

    }






    尝试2
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char s[10000][256];
    
    int cmp ( const void *a , const void *b )
    {   
      char *x=(char *)a,*y=(char *)b;
    if ( strlen(x)!=strlen(y) ) return  strlen(x)-strlen(y);  
    }
    
    
    int main(int argc, char *argv[])
    {   int i,n;
     n=0;
     while ( scanf("%s",s[n])!=EOF && s[n][0]!='#' ) n++; 
      qsort(s,n,sizeof(s[0]),cmp);
        for (i=0; i<n; i++)
         printf("%s
    ",s[i]);     
     return 0;
    }
    View Code

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    char s[10000][256];

    int cmp ( const void *a , const void *b )

    {    

    char *x=(char *)a,*y=(char *)b;

    if ( strlen(x)!=strlen(y) )                  return  strlen(x)-strlen(y);        

         }

    int main(int argc, char *argv[])

      int i,n;  

    n=0;  

    while ( scanf("%s",s[n])!=EOF && s[n][0]!='#' )      n++;  

    qsort(s,n,sizeof(s[0]),cmp);  

       for (i=0; i<n; i++)     

    printf("%s ",s[i]);    

     return 0;

    }

    尝试 3

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char s[10000][256];
    
    int cmp ( const void *a , const void *b )
    {   
      char *x=(char *)a,*y=(char *)b;
      int i=0,j=0;
    if ( strlen(x)!=strlen(y) ) return  strlen(x)-strlen(y); 
    
    while(x[i]) i++;
    while(y[j]) j++;
    
    if(i==j) return strcmp(x,y);
    if(i!=j) return i-j; 
    }
    
    
    int main(int argc, char *argv[])
    {   int i,n;
     n=0;
     while ( scanf("%s",s[n])!=EOF && s[n][0]!='#' ) n++; 
      qsort(s,n,sizeof(s[0]),cmp);
        for (i=0; i<n; i++)
         printf("%s
    ",s[i]);     
     return 0;
    }
    View Code

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    char s[10000][256];

    int cmp ( const void *a , const void *b )

    {       

     char *x=(char *)a,*y=(char *)b;  

            int i=0,j=0;

    if ( strlen(x)!=strlen(y) )            return  strlen(x)-strlen(y);

                   while(x[i])      i++;

                 while(y[j])       j++;

    if(i==j)           return strcmp(x,y);

    if(i!=j)              return i-j;

    }

    int main(int argc, char *argv[])

      int i,n;  n=0;  

    while ( scanf("%s",s[n])!=EOF && s[n][0]!='#' )     n++;

      qsort(s,n,sizeof(s[0]),cmp);   

      for (i=0; i<n; i++)   

       printf("%s ",s[i]);  

        return 0;

    }




    尝试4



    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char s[10000][256];
    
    int cmp ( const void *a , const void *b )
    {   
      char *x=(char *)a,*y=(char *)b;
      int i=0,j=0;
      while(x[i]) i++;
    while(y[j]) j++;
    
    if ( strlen(x)!=strlen(y) ) return  strlen(x)-strlen(y); 
    else if ( strlen(x)==strlen(y) ) return i-j; 
    
    
    
    else return strcmp(x,y);
     
    }
    
    
    int main(int argc, char *argv[])
    {   int i,n;
     n=0;
     while ( scanf("%s",s[n])!=EOF && s[n][0]!='#' ) n++; 
      qsort(s,n,sizeof(s[0]),cmp);
        for (i=0; i<n; i++)
         printf("%s
    ",s[i]);     
     return 0;
    }
    View Code

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    char s[10000][256];

    int cmp ( const void *a , const void *b )

    {    

    char *x=(char *)a,       *y=(char *)b;  

    int i=0,j=0;  

    while(x[i]) i++;

    while(y[j]) j++;

      if ( strlen(x)!=strlen(y) )                             return  strlen(x)-strlen(y);

          else    if ( strlen(x)==strlen(y) )                 return i-j;

                        else                              return strcmp(x,y);  

    }

    int main(int argc, char *argv[])

    {

       int i,n;

     n=0;  

    while ( scanf("%s",s[n])!=EOF && s[n][0]!='#' )            n++;  

    qsort(s,n,sizeof(s[0]),cmp);  

       for (i=0; i<n; i++)     

    printf("%s ",s[i]);    

     return 0;

    }

    ***********************************************************************************************************************************************

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char s[10000][256];
    
    
    int cmp ( const void *a , const void *b )
    {   

    char *x=(char *)a,*y=(char *)b; int i=0,j=0,k; k=0; while(x[k]) {if(x[k]=='1') i++; k++;} k=0; while(y[k]) {if(y[k]=='1') j++; k++;} if ( strlen(x)!=strlen(y) ) return strlen(x)-strlen(y); //串长度 是否 相等 if (i!=j) return i-j; // 1 的个数 比较 return strcmp(x,y); //ASC||码 比较 } int main(int argc, char *argv[]) { int i,n; n=0; while ( scanf("%s",s[n])!=EOF ) n++; qsort(s,n,sizeof(s[0]),cmp); for (i=0; i<n; i++) printf("%s ",s[i]); return 0; } ******************************** #include <stdio.h> #include <stdlib.h> #include <string.h> char s[10000][256]; int one(char *x) { int c=0,i=0; while (x[i]!='') { if (x[i]=='1') c++; i++; } return c; } int cmp ( const void *a , const void *b ) { char *x=(char *)a,*y=(char *)b; int i=one(x),j=one(y),k; if ( strlen(x)==strlen(y) ) if (i==j) return strcmp(x,y); else return i-j; else return strlen(x)-strlen(y); } int main(int argc, char *argv[]) { int i,n; n=0; while ( scanf("%s",s[n])!=EOF ) n++; qsort(s,n,sizeof(s[0]),cmp); for (i=0; i<n; i++) printf("%s ",s[i]); return 0; } ********************************************** #include <stdio.h> #include <stdlib.h> #include <string.h> char s[10000][256]; int one(char *x) { int c=0,i=0; while (x[i]!='') { if (x[i]=='1') c++; i++; } return c; } int cmp ( const void *a , const void *b ) { char *x=(char *)a,*y=(char *)b; int i=one(x),j=one(y),k; if ( strlen(x)!=strlen(y) ) return strlen(x)-strlen(y); if (i!=j) return i-j; return strcmp(x,y); } int main(int argc, char *argv[]) { int i,n; n=0; while ( scanf("%s",s[n])!=EOF ) n++; qsort(s,n,sizeof(s[0]),cmp); for (i=0; i<n; i++) printf("%s ",s[i]); return 0; }

    ****************************************************************************************************************************************************

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    char s[10000][256];

    int cmp ( const void *a , const void *b )

    {  

    char *x=(char *)a,     *y=(char *)b;

    int i=0,j=0,k;

    k=0; 

         while(x[k])      i=i+x[k]-'0',        k++  ;

    k=0 ;

          while(y[k])    j=j+y[k]-'0',        k++   ;

       if ( strlen(x)==strlen(y) )       

             if (i==j)     return strcmp(x,y);      

               else         return i-j;    

         else    return  strlen(x)-strlen(y);

    }

    int main(int argc, char *argv[])

      int i,n;

     n=0;

     while ( scanf("%s",s[n])!=EOF  )    n++;  

    qsort(s,n,sizeof(s[0]),cmp);    

    for (i=0; i<n; i++)     

    printf("%s ",s[i]);    

     return 0;

    }

    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <set>
    #include <algorithm>
    using namespace std;
    
    struct Comp
    {
        bool operator ()(const string &s1,const string &s2)
        {
            if(s1.length()!=s2.length()) return s1.length()<s2.length();
            int c1=count(s1.begin(),s1.end(),'1');
            int c2=count(s2.begin(),s2.end(),'1');
            return (c1!=c2?c1<c2:s1<s2);
        }
    };
    int main()
    {
        multiset<string,Comp>ms;
        string s;
        while(cin>>s)
        {
            ms.insert(s);
        }
        for(multiset<string,Comp>::iterator it=ms.begin();it!=ms.end();it++)
        {
            cout<<*it<<endl;
        }
        return 0;
    }
  • 相关阅读:
    浅谈iOS的SDK与API
    ulua-应用层模块编码
    ErlangRoad_2
    ErlangRoad_1
    Git笔记--git使用基本概念和术语
    小米Pro 15.6 系统重装记录
    Ubuntu14.04安装opencv2.4.13
    ubuntu配置机器学习环境(四) 安装intel MKL
    ubuntu配置机器学习环境(三) opencv 安装
    ubuntu配置机器学习环境(二) cuda 和cudnn 安装
  • 原文地址:https://www.cnblogs.com/2014acm/p/3876433.html
Copyright © 2011-2022 走看看