http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=3
1.3.1 FatMouse' Trade #include <algorithm> /* 题意:价值/代价的比值来排序,买比值大的。 Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 */ #include<stdio.h> #include<stdlib.h> const int MAXN = 1010; struct node { double j,f; double r; }a[MAXN]; int cmp(const void *a,const void *b) { struct node *c=(node *)a; struct node *d=(node *)b; if(c->r > d->r) return -1; else return 1; } int main() { int N; double M; double ans; while(scanf("%lf%d",&M,&N)) { if(M==-1&&N==-1) break; for(int i=0;i<N;i++) { scanf("%lf%lf",&a[i].j,&a[i].f); a[i].r=(double)a[i].j/a[i].f; } qsort(a,N,sizeof(a[0]),cmp); ans=0; for(int i=0;i<N;i++) { if(M>=a[i].f) { ans+=a[i].j; M-=a[i].f; } else { ans+=(a[i].j/a[i].f)*M; break; } } printf("%.3lf\n",ans); } return 0; }
1.3.2 今年暑假不AC * Sample Input 12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0 Sample Output 5 */ #include <stdio.h> #include <stdlib.h> #include <string.h> struct ti { int s, e; }; int compare(const void *a, const void *b); int main() { int i, n, k; struct ti tis[101], temp[101]; while(scanf("%d", &n) != EOF) { if(n == 0) break; for(i = 0; i < n; i ++) { scanf("%d %d", &tis[i].s, &tis[i].e); } qsort(tis, n, sizeof(tis[0]), compare); k = 0; temp[k] = tis[0]; for(i = 1; i < n; i ++) { if(tis[i].s >= temp[k].e) temp[++ k] = tis[i]; } printf("%d\n", k + 1); } return 0; } int compare(const void *a, const void *b) { const struct ti *p = (ti*)a; const struct ti *q = (ti*)b; return p->e - q->e; }
1.3.3 排名 #include <string> #include <stdio.h> #include <algorithm> using namespace std; #define N 1000 int que[10]; struct node { char name[20]; int num; int score; }stu[N]; bool cmp(const node& a, const node& b) { if (a.score == b.score) { return strcmp(a.name, b.name) < 0 ? 1:0; } else { return a.score > b.score; } } /* 联系字典序:第1行给出考生人数N ( 0 < N < 1000 )、考题数M ( 0 < M < = 10 )、分数线(正整数)G; 第2行排序给出第1题至第M题的正整数分值; 以下N行,每行给出一名考生准考证号(长度不超过20的字符串)、该生解决的题目总数m、以及这m道题的题号 4 5 25 10 10 12 13 15 CS004 3 5 1 3 CS003 5 2 4 1 3 5 CS002 2 1 2 CS001 3 2 3 5 */ int main() { int student, question, judge, x, count; while(scanf("%d", &student),student) { count = 0; for (int i = 1; i <= student;++i) { stu[i].score = 0; stu[i].num = 0; } scanf("%d%d",&question, &judge); for (int i = 1;i <= question;++i) { scanf("%d",&que[i]); } for (int i = 1;i <= student;++i) { scanf("%s%d",&stu[i].name,&stu[i].num); while(stu[i].num--) { scanf("%d",&x); stu[i].score += que[x]; } if (stu[i].score >= judge) count ++; } sort(stu+1,stu+1+student,cmp); printf("%d\n",count); for (int i = 1;i <= student;++i) { if (stu[i].score >= judge) printf("%s %d\n",stu[i].name, stu[i].score); else break; } } return 0; }
1.3.4 开门人和关门人 #include "stdafx.h" #include <iostream> #include <string> using namespace std ; struct node { string name, timee; }maxt, mint;//记录最大和最小的结构体 int main() { int t, n; string s,mis, mas; cin>>t; while (t--) { cin>>n; n--; cin>>s>>mint.timee>>maxt.timee; mint.name = maxt.name = s; while (n--) { cin>>s>>mis>>mas; if (mis < mint.timee) { mint.name = s; mint.timee = mis; } if (mas > maxt.timee) { maxt.name = s; maxt.timee = mas; } } cout<<mint.name<<" "<<maxt.name<<endl; } return 0; }