1.冒泡排序
1 #include <stdio.h> 2 int main(){ 3 int i,j,n,temp; 4 int a[100]; 5 while(scanf("%d",&n)!=EOF){ 6 for(int i=0;i<n;i++){ 7 scanf("%d",&a[i]); 8 } 9 for(i=0;i<n-1;i++){ 10 for(j=0;j<n-i-1;j++){ 11 if(a[j]>a[j+1]){ 12 temp = a[j]; 13 a[j]=a[j+1]; 14 a[j+1] = temp; 15 } 16 } 17 } 18 for(i=0;i<n;i++){ 19 printf("%d ",a[i]); 20 } 21 printf(" "); 22 } 23 return 0; 24 }
2.qsort实现快速排序--优点:不易出错,速度快
排序-qsort
3.结构体排序(成绩排序)---2000清华复试
将学生按照成绩排序,成绩相同则按照学生名字,若仍相同,按照年龄;
样例输入: 对应输出
3
abc 18 93 abc 18 93
ecd 19 97 ecd 19 97
ecd 20 97 ecd 20 97
4.日期差值
样例输入 对应输入
20130101 5
20130105
1 #include <stdio.h> 2 #include <math.h> 3 int yeap(year){ 4 return (year % 4==0&& year % 100 !=0)||(year % 400 == 0); 5 } 6 int day(int Y, int M, int D){ 7 int distance = 0; 8 int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; 9 if(yeap(Y)) month[1] = 29; 10 for(int i=1; i<M; i++){ 11 distance = distance + month[i-1]; 12 } 13 return (distance + D); 14 } 15 int main(int argc, char const *argv[]){ 16 int y1,m1,d1,y2,m2,d2,lab; 17 while(scanf("%4d%2d%2d",&y1,&m1,&d1)!= EOF){ 18 scanf("%4d%2d%2d",&y2,&m2,&d2); 19 int result = 0; 20 if(y1!=y2){ 21 for(int i=y1;i<y2;i++){ 22 printf("%d-%d ",i,i+1 ); 23 if(yeap(i)) result += 366; 24 else result += 365; 25 } 26 } 27 result = result + day(y2, m2,d2) - day(y1,m1,d1); 28 printf("%d ", result); 29 } 30 return 0; 31 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 typedef struct Student{ 5 char name[100]; 6 int age; 7 int score; 8 }stu; 9 int compfunc(void const *a, void const *b){ 10 if((*(stu *)a).score!=(*(stu *)b).score) return (*(stu *)a).score-(*(stu *)b).score; 11 int temp = strcmp((*(stu *)a).name,(*(stu *)b).name); 12 if(temp!=0) return temp < 0; 13 else return (*(stu *)a).age-(*(stu *)b).age; 14 } 15 int main(int argc, char const *argv[]){ 16 int n,i,j; 17 stu mates[100]; 18 while(scanf("%d",&n)!=EOF){ 19 for (int i = 0; i < n; ++i){ 20 scanf("%s %d %d", mates[i].name, &mates[i].age, &mates[i].score); 21 } 22 qsort(mates,n,sizeof(int)*2+sizeof(char)*100,compfunc); 23 for (int i = 0; i < n; ++i){ 24 printf("%s %d, %d ", mates[i].name, mates[i].age, mates[i].score); 25 } 26 } 27 return 0; 28 }