hdu 1859
#include <stdio.h> struct Point{ int x,y; }min,max,tmp; int main(void) { while( scanf("%d %d",&tmp.x, &tmp.y), tmp.x!=0 || tmp.y!=0 ) { min.x = tmp.x; min.y = tmp.y; max.x = tmp.x; max.y = tmp.y; while(scanf("%d %d",&tmp.x, &tmp.y), tmp.x!=0 || tmp.y!=0) { if(tmp.x < min.x) min.x = tmp.x; if(tmp.x > max.x) max.x = tmp.x; if(tmp.y < min.y) min.y = tmp.y; if(tmp.y > max.y) max.y = tmp.y; } printf("%d %d %d %d\n", min.x,min.y,max.x,max.y); } return 0; }
hdu 1860
#include <stdio.h> #include <string.h> char charSet[128]; int main(void) { char buf1[6]; char buf2[83]; int len = 0; while( gets(buf1), buf1[0] != '#' ) { memset(charSet, -1, sizeof(charSet)); len = strlen(buf1); for(int i=0; i<len; i++) charSet[ buf1[i] ] = 0; gets(buf2); len = strlen(buf2); for(int i=0; i<len; i++) { if( charSet[ buf2[i] ] == -1 ) continue; else charSet[ buf2[i] ]++; } len = strlen(buf1); for(int i=0; i<len; i++) { printf("%c %d\n",buf1[i], charSet[buf1[i]]); } } return 0; }
HDU1862
这题虽然是水题,但是让我复习了下qsort函数的用法,本来就会对数组排个序,现在知道怎么对结构体进行二级排序了,具体参考另一篇文章:
sort和qsort函数的用法
AC代码
#include <stdio.h> #include <stdlib.h> #include <string.h> struct excel{ int num; char name[10]; int score; }table[100000]; int cmp1(const void *a, const void *b) { return ((excel *)a)->num - ((excel *)b)->num; } int cmp2(const void *a, const void *b) { if(strcmp( ((excel *)a)->name, ((excel *)b)->name ) != 0) { return strcmp( ((excel *)a)->name, ((excel *)b)->name ); } return ((excel *)a)->num - ((excel *)b)->num; } int cmp3(const void *a, const void *b) { if( ((excel *)a)->score != ((excel *)b)->score ) return ((excel *)a)->score - ((excel *)b)->score; return ((excel *)a)->num - ((excel *)b)->num; } int Case=0; int main(void) { int n,c; while(scanf("%d %d", &n, &c), n!=0 ) { for(int i=0; i<n; i++) { scanf("%d %s %d", &table[i].num, &table[i].name, &table[i].score); } switch(c) { case 1: qsort(table, n, sizeof(table[0]), cmp1); break; case 2: qsort(table, n, sizeof(table[0]), cmp2); break; case 3: qsort(table, n, sizeof(table[0]), cmp3); break; } printf("Case %d:\n", ++Case); for(int i=0; i<n; i++) { printf("%06d %s %d\n", table[i].num, table[i].name, table[i].score); } } return 0; }
下面这个是用sort函数写的1862,开始用了比较多的cin和cout,执行时间比之前的慢了很多很多!后面又改回去了,这里充分体会到了输入输出时用scanf和printf的好处。
在其他不变的情况下,把qsort改为sort会快些
#include <algorithm> #include <stdio.h> using namespace std; struct excel{ int num; char name[10]; int score; }table[100000]; bool cmp1(excel a, excel b) { return a.num < b.num; } bool cmp2(excel a, excel b) { if(strcmp( a.name, b.name ) != 0) { return strcmp( a.name, b.name ) < 0; } return a.num < b.num; } bool cmp3(excel a, excel b) { if( a.score != b.score ) return a.score < b.score; return a.num < b.num; } int Case=0; int main(void) { int n,c; while(scanf("%d %d", &n, &c), n!=0 ) { for(int i=0; i<n; i++) { scanf("%d %s %d", &table[i].num, &table[i].name, &table[i].score); } switch(c) { case 1: sort(table, table + n, cmp1); break; case 2: sort(table, table + n, cmp2); break; case 3: sort(table, table + n, cmp3); break; } printf("Case %d:\n", ++Case); for(int i=0; i<n; i++) { printf("%06d %s %d\n", table[i].num, table[i].name, table[i].score); //cout << setw(6) << setfill('0')<< table[i].num <<" "<< table[i].name <<" "<< table[i].score<<endl; } } return 0; }