一,2005年 开门人和关门人(http://acm.hdu.edu.cn/showproblem.php?pid=1234)
直接用sort或者qsort排序两次就可以分别找到开门人和关门人。
又是那种情况,下一轮再做一遍,再贴代码。
二,2005年 排名(http://acm.hdu.edu.cn/showproblem.php?pid=1236)
结构体多级排序。直接用sort或者qsort就可以。
很莫名其妙,就是一个printf();在for循环,和for循环外面的的位置不同,害我找了半天的错误。等下要研究一下,个人觉得没有区别呀!倒~~
#include<stdio.h> #include<algorithm> using namespace std; struct Student { char num[23]; int sumscore; }; int N,M,G; int score[12]; Student students[1004]; bool cmp(Student a,Student b) { if( a.sumscore != b.sumscore ) return a.sumscore > b.sumscore; else return strcmp( a.num , b.num ) < 0; } int main() { int a,b,c; while(scanf("%d",&N)&&N!=0) { scanf("%d%d",&M,&G); for(int i=1;i<=M;i++) { scanf("%d",&a); score[i]=a; } for(int j=0;j<N;j++) { scanf("%s%d",students[j].num,&b); // students[j].sumscore=0; while(b--) { scanf("%d",&c); students[j].sumscore+=score[c]; } } sort(students,students+N,cmp); for(int k=0;k<N;k++) { if(students[k].sumscore<G) {break;} } printf("%d/n",k); //居然是这里 N=k; for(k=0;k<N;k++) { printf("%s %d/n",students[k].num,students[k].sumscore); } } return 0; }
三,2007年 EXCEL排序(http://acm.hdu.edu.cn/showproblem.php?pid=1862)
感觉做得比较顺手,也就是那个SORT方法。
#include<stdio.h> #include<algorithm> using namespace std; struct Student { char number[8]; char name[10]; int score; }; Student students[100003]; int N,C; bool cmp1(Student s1,Student s2) //C=1 { return strcmp(s1.number,s2.number)<0; } bool cmp2(Student s1,Student s2)//C=2 { if(strcmp(s1.name,s2.name)==0) { return strcmp(s1.number,s2.number)<0; } else if(strcmp(s1.name,s2.name)<0) return true; return false; } bool cmp3(Student s1,Student s2)//C=3 { if(s1.score == s2.score) { return strcmp(s1.number,s2.number)<0; } else if(s1.score < s2.score) return true; return false; } int main() { int i; static int j=0; while(scanf("%d %d",&N,&C)&&N!=0) { for(i=0;i<N;i++) { scanf("%s %s %d",&students[i].number,&students[i].name,&students[i].score); } switch(C) { case 1:sort(students,students+N,cmp1);break; case 2:sort(students,students+N,cmp2);break; case 3:sort(students,students+N,cmp3);break; default:printf("input error/n");break; } j++; printf("Case %d:/n",j); for(i=0;i<N;i++) { printf("%s %s %d/n",students[i].number,students[i].name,students[i].score); } } return 0; }
四,2008年 魔咒词典(http://acm.hdu.edu.cn/showproblem.php?pid=1880)
主要是对字符串的处理方面的问题,对于读取带空格的字符串用的是gets方法,对字符串进行裁剪用的是strncpy方法。调试时,发现一个问题,对于不是用于读入数据的数组,在使用之前必须初始化。以后得好好记住了。其实这到题也就是考对字符串的处理,别的也没好多,以后碰见对带空格字符串可以参照这个题目。
#include<iostream> #include<algorithm> #include<string> using namespace std; struct Spelldictionary { char spell[22]; char function[83]; }; Spelldictionary spelldictionarys1[100003]; Spelldictionary spelldictionarys2[100003]; int i; bool cmp1(Spelldictionary s1,Spelldictionary s2) { return strcmp(s1.spell,s2.spell)<0; } bool cmp2(Spelldictionary s1,Spelldictionary s2) { return strcmp(s1.function,s2.function)<0; } void Search(char ch[],bool flg) { int low,high,mid; low=0;high=i-1; while(low<=high) { mid = (low+high)/2; if(flg) { if(strcmp(ch,spelldictionarys1[mid].spell) == 0) { cout<<spelldictionarys1[mid].function<<endl;break; } else if(strcmp(ch,spelldictionarys1[mid].spell)>0) { low = mid+1; } else { high = mid-1; } } else { if(strcmp(ch,spelldictionarys2[mid].function) == 0) { cout<<spelldictionarys2[mid].spell<<endl;break; } else if(strcmp(ch,spelldictionarys2[mid].function)>0) { low = mid+1; } else { high = mid-1; } } } if(low>high)cout<<"what?"<<endl; } int main() { char ch1[105],ch2[105]; int n; /*char ch[15]={"dafgag"}; char chw[15]={""}; strncpy(chw,ch+1,strlen(ch)-2); cout<<chw<<endl;*/ i=0; while(gets(ch1)&&strcmp(ch1,"@END@")!=0&&i<100000) { for(int j=0;j<strlen(ch1);j++) { if(ch1[j]==']')break; } strncpy(spelldictionarys1[i].spell,ch1+1,j-1); strcpy(spelldictionarys2[i].spell,spelldictionarys1[i].spell); strncpy(spelldictionarys1[i].function,ch1+2+j,strlen(ch1)-j-2); strcpy(spelldictionarys2[i].function,spelldictionarys1[i].function); i++; } sort(spelldictionarys1,spelldictionarys1+i,cmp1); sort(spelldictionarys2,spelldictionarys2+i,cmp2); cin>>n; gets(ch2); while(n--) { char ch3[22]={""}; //初始化 remember gets(ch2); if(ch2[0]=='[') { int len = strlen(ch2); strncpy(ch3,ch2+1,len-2); Search(ch3,true); } else { Search(ch2,false); } } return 0; }