标签:
题库:http://ac.jobdu.com/tutorialproblems.php
题号1202排序
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 /*递增排序*/ 6 int compInc(const void *a,const void *b) 7 { 8 return *(int *)a-*(int *)b; 9 } 10 11 int main() 12 { 13 int n,*A; 14 while(scanf("%d",&n)!=EOF) 15 { 16 A = (int *)malloc(sizeof(int)*n); 17 int i; 18 if(n<1||n>100) 19 return 0; 20 else 21 { 22 for(i=0;i<n;i++) 23 { 24 scanf("%d",A+i); 25 } 26 qsort(A,n,sizeof(*A),compInc); 27 for(i=0;i<n;i++) 28 { 29 printf("%d ",*(A+i)); 30 } 31 printf("\n"); 32 } 33 } 34 return 0; 35 }
题号1061成绩排序
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 typedef struct S{ 6 char name[20]; 7 int age; 8 int grade; 9 }student; 10 11 int cmp(const void *a,const void *b) //实现比较规则 12 { 13 student * aa = (student *)a; 14 student * bb = (student *)b; 15 if(aa->grade!=bb->grade) 16 return aa->grade-bb->grade;//若分数不相同则分数低者在前 17 else if(strcmp(aa->name,bb->name)!=0) 18 return (strcmp(aa->name,bb->name));//若分数相同则名字字典序小者在前 19 else return aa->age-bb->age;//若名字也相同则年龄小者在前 20 } 21 22 int main() 23 { 24 int n; 25 int i; 26 student a[1000]; 27 while(scanf("%d",&n)!=EOF) 28 { 29 for(i=0;i<n;i++) 30 { 31 scanf("%s%d%d",a[i].name,&a[i].age,&a[i].grade); 32 } 33 qsort(a,n,sizeof(student),cmp); 34 for(i=0;i<n;i++) 35 { 36 printf("%s %d %d\n",a[i].name ,a[i].age ,a[i].grade ); 37 } 38 } 39 return 0; 40 }
题号1061成绩排序(与上题定义结构的方法不同)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 struct S{ 6 char name[20]; 7 int age; 8 int grade; 9 }student[1000]; 10 11 int cmp(const void *a,const void *b) //实现比较规则 12 { 13 struct S *aa = (struct S *)a; 14 struct S *bb = (struct S *)b; 15 int tmp=strcmp(aa->name,bb->name); 16 if(aa->grade!=bb->grade) return aa->grade>bb->grade?1:-1;//若分数不相同则分数低者在前 17 else if(tmp!=0) return tmp;//若分数相同则名字字典序小者在前 18 else return aa->age<bb->age?1:-1;//若名字也相同则年龄小者在前 19 } 20 21 int main() 22 { 23 int n; 24 int i; 25 while(scanf("%d",&n)!=EOF) 26 { 27 for(i=0;i<n;i++) 28 { 29 scanf("%s%d%d",student[i].name,&student[i].age,&student[i].grade); 30 } 31 qsort(student,n,sizeof(student[0]),cmp); 32 for(i=0;i<n;i++) 33 { 34 printf("%s %d %d\n",student[i].name ,student[i].age ,student[i].grade ); 35 } 36 } 37 return 0; 38 }
题号1185特殊排序
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int cmp(const void *a,const void *b) 5 { 6 return *(int *)a-*(int *)b; 7 } 8 9 int main() 10 { 11 int n,i,*A; 12 while(scanf("%d",&n)!=EOF) 13 { 14 A = (int *)malloc(sizeof(int)*n); 15 for(i=0;i<n;i++) 16 { 17 scanf("%d",A+i); 18 } 19 if(n==1) 20 printf("%d\n-1",*A); 21 else 22 { 23 qsort(A,n,sizeof(*A),cmp); 24 printf("%d\n",*(A+n-1)); 25 for(i=0;i<n-2;i++) 26 { 27 printf("%d ",*(A+i)); 28 } 29 printf("%d",*(A+n-2));//防止多输出一个空格 30 } 31 printf("\n");//这个换行符使光标移到下一行 没有它会wrong answer 32 } 33 return 0; 34 }
题号1023EXCEL排序
(更新中...)
标签:
原文地址:http://www.cnblogs.com/Rivrr/p/5162563.html