5.18刷题
田忌赛马
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=3&problemid=4
1 //贪心算法 2 //据题目要求,有三种可能:1.上等马和上等马比,2.下等马和下等马比,3. 下等马和上等马比较 3 //选出最有利于田忌的一种情况 4 //每次比较完毕,指针后移或前移,表示已经比较过了,并统计胜负(sum) 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 const int maxn=1010; 9 int T[maxn]; 10 int Q[maxn]; 11 bool cmp(int a,int b) 12 { 13 return a>b; 14 } 15 int main() 16 { 17 int n; 18 while(cin>>n,n) 19 { 20 int sum=0; 21 for(int i=0;i<n;i++) 22 { 23 cin>>T[i]; 24 } 25 for(int i=0;i<n;i++) 26 { 27 cin>>Q[i]; 28 } 29 sort(T,T+n,cmp);//贪心老规矩先排序 30 sort(Q,Q+n,cmp); 31 //指针初始化 32 int qf=0,tf=0; 33 int qe=n-1,te=n-1; 34 while(n--)//n匹马,比较n次 35 { 36 if(T[tf]>Q[qf])//上等马和上等马 37 { 38 tf++; 39 qf++; 40 sum++; 41 } 42 else if(T[te]>Q[qe])//下等马和下等马 43 { 44 te--; 45 qe--; 46 sum++; 47 } 48 else if(T[te]<Q[qf])//下等马和上等马 49 { 50 te--; 51 qf++; 52 sum--; 53 } 54 } 55 56 cout<<sum*200<<endl; 57 } 58 return 0; 59 }
百步穿杨
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=3&problemid=6
1 //贪心,只有排序,没什么好说的,甚至不用剪 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=60; 6 struct node 7 { 8 int len; 9 int num; 10 }; 11 node a[maxn]; 12 bool cmp(node x,node y) 13 { 14 return x.len<y.len; 15 } 16 int main() 17 { 18 int t; 19 int n; 20 cin>>t; 21 while(t--) 22 { 23 cin>>n; 24 for(int i=0;i<n;i++) 25 { 26 cin>>a[i].len>>a[i].num; 27 } 28 sort(a,a+n,cmp); 29 for(int i=0;i<n;i++) 30 { 31 while(a[i].num--) 32 { 33 cout<<">"<<"+"; 34 for(int j=1;j<=a[i].len-2;j++) 35 { 36 cout<<"-"; 37 } 38 cout<<"+"<<">"<<endl; 39 } 40 cout<<endl; 41 } 42 } 43 return 0; 44 }
考试排名
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=3&problemid=12
//注意看题目要求的数据范围,有时候开太大会被卡 //结构体+排序,这里用冒泡法 //重点在输出,怎么合理处理括号的输入,这里用getchar进行判断和处理 //由于输入问题,所以不会停止,加system("pause")停止 #include<iostream> #include<cstring> #include<stdio.h> using namespace std; struct node { int num;//解决题目数 int f;//时间 char name[10];//姓名 int solve[13];//每题的时间 }; node a[1000]; int main() { int n,m; char b[10]; int k=0; cin>>n>>m; while(cin>>b)//输入姓名 { a[k].f=0; a[k].num=0; strcpy(a[k].name,b); for(int i=0;i<n;i++) { cin>>a[k].solve[i]; if(a[k].solve[i]>0) { a[k].num++; a[k].f+=a[k].solve[i]; } if(getchar()=='(')//处理和判定括号,本题的最大注意点 { int x; cin>>x; a[k].f+=x*m; getchar(); } }k++;//控制第几个人 } for(int i=0;i<k;i++)//冒泡排序 { for(int j=0;j<=k-i-1;j++) { node t; if(a[j].num<a[j+1].num)//题目数 { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } else if(a[j].num==a[j+1].num) { if(a[j].f>a[j+1].f)//时间 { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } else if(a[j].num==a[j+1].num) { if(a[j].name>a[j+1].name)//字母 { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } } } for(int i=0;i<k;i++) { printf("%-10s %2d %4d\n",a[i].name,a[i].num,a[i].f); } system("pause");//系统暂停 return 0; }

浙公网安备 33010602011771号