您的位置:首页 > 其它

九度oj 文献排序

2012-10-25 10:46 232 查看

起因:

九度有些题目是在线测试题目,没有讨论区,这道三星题目我看AC率还是挺低的,所以贴出自己的AC代码,供大家学习讨论

考察点:

考察点在结构体的使用,用了一个数组的复制,同时注意字符串的结束符为'\0'

题目描述:
现在你的导师给你了一个待排序的参考文献列表,要你排好序给他。

文献列表中每一条文献记录只占一行。排序的规则和string类型字符串的比较规则一致(在排序时如果该字符串中包含大写字母,则当作小写字母处理,保证没有相同大小的字符串,但是输出结果不能改变任一字符串),按升序排列。

输入:
输入包括多组,每组输入第一行包括一个整数n,(1<=n<=200),接下来有n行,每行包括一行文献记录,文献记录的长度s(1<=s<=200)。

输出:
        对每组输入。输出排好序的文献记录。

样例输入:
3abc hello!

Abc hellz!

bbc hello!

样例输出:
abc hello!

Abc hellz!

bbc hello!

AC代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

struct literature
{
char str1[201];
char str2[201];
};

int partition(struct literature *A, int left, int right);
void  quicksort(struct literature *A, int begin, int end);

int main()
{
int n, i, j;
struct literature docus[201];
char ch;

while(scanf("%d",&n) != EOF)
{
//吸收回车符
ch = getchar();
//接收文献数据
for(i = 0; i < n; i ++)
{
gets(docus[i].str1);
}

//给区分大小写的结构体中数组赋值
for(i = 0; i < n; i ++)
{
for(j = 0; docus[i].str1[j] != '\0'; j ++)
{
if(isalpha(docus[i].str1[j]))
{
docus[i].str2[j] = tolower(docus[i].str1[j]);
}else
{
docus[i].str2[j] = docus[i].str1[j];
}
}
docus[i].str2[j] = '\0';
}

//快速排序
quicksort(docus, 0, n - 1);

//打印输出
for(i = 0; i < n; i ++)
{
printf("%s\n",docus[i].str1);
}
}

return 0;
}

void quicksort(struct literature *A, int begin, int end)
{
int pivot;

if(begin < end)
{
pivot = partition(A, begin, end);
quicksort(A, begin, pivot - 1);
quicksort(A, pivot + 1, end);
}
}

int partition(struct literature *A, int left, int right)
{
struct literature stand = A[left];

while(left < right)
{
while(left < right && strcmp(A[right].str2,stand.str2) >= 0)
{
right --;
}
if(left < right)
{
A[left ++] = A[right];
}
while(left < right && strcmp(A[left].str2,stand.str2) <= 0)
{
left ++;
}
if(left < right)
{
A[right --] = A[left];
}

}

A[left] = stand;
return left;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: