Fork me on GitHub


题目1419:文献排序
时间限制:1 秒内存限制:32 兆特殊判题:否提交:1445解决:371
题目描述:
现在你的导师给你了一个待排序的参考文献列表,要你排好序给他。
文献列表中每一条文献记录只占一行。排序的规则和string类型字符串的比较规则一致(在排序时如果该字符串中包含大写字母,则当作小写字母处理,保证没有相同大小的字符串,但是输出结果不能改变任一字符串),按升序排列。
输入:
输入包括多组,每组输入第一行包括一个整数n,(1<=n<=200),接下来有n行,每行包括一行文献记录,文献记录的长度s(1<=s<=200)。
输出:
        对每组输入。输出排好序的文献记录。
样例输入:
3
abc hello!
Abc hellz!
bbc hello!
样例输出:
abc hello!
Abc hellz!
bbc hello!

 


#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct{int i;char *x,*y;}S;
int cmp(const void*a,const void*b){return strcmp(((S*)a)->y,((S*)b)->y);}
int main(){
    int N,i,l;char t[201];S* R;
    while(~scanf("%d",&N)){
        R=(S*)malloc(N*sizeof(S));
        i=N;gets(t);
        while(i--){
            gets(t);
            R[i].i=i;l=strlen(t)+1;
            R[i].x=(char*)malloc(l);
            R[i].y=(char*)malloc(l);
            strcpy(R[i].x,t);
            while(l--)R[i].y[l]=tolower(R[i].x[l]);         
        }
        qsort(R,N,sizeof(S),cmp);
        for(i=0;i<N;++i){puts(R[i].x);free(R[i].x);free(R[i].y);}
        free(R);
    }
}
/**************************************************************
    Problem: 1419
    User: 6767227
    Language: C++
    Result: Accepted
    Time:50 ms
    Memory:1012 kb
***********************
#include<cstdio>
#include<cstring>
#include<cctype>
#include <string>
#include<algorithm>
#include <cstdlib>
using namespace std;
class node
{
public:string a;
};
bool cmp(node a,node b)
{
    int i;
    string tmpa=a.a,tmpb=b.a;
    for(i=0;i<tmpa.size();i++) 
      tmpa[i]=tolower(tmpa[i]);
        for(i=0;i<tmpb.size();i++) 
      tmpb[i]=tolower(tmpb[i]);
        return  tmpa.compare(tmpb)<0?1:0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int i,j,n ;char a[201];node cla[201];
    while(~scanf("%d",&n ))
    {
        getchar();
        for(i=0;i<n;i++)
        {
            j=0;
            memset(a,0,sizeof a);
            while(~scanf("%c",&a[j])) 
            {if(a[j]=='\n') break;j++;}
            a[j]='\0';
//    printf("%s\n",a);
            cla[i].a=a;
        }
        std::sort(cla,cla+n,cmp);
    for(i=0;i<n;i++)
        printf("%s\n",cla[i].a.c_str());
        
    }
    
    
    return 0;
}

 

posted on 2013-03-04 22:05  huashiyiqike  阅读(357)  评论(0编辑  收藏  举报