CodeForce---Educational Codeforces Round 3 USB Flash Drives (水题)解题报告

对于这题明显是用贪心算法来解决问题:

下面贴出笔者的代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 int a[105];
 7 bool cmp(int A,int B)
 8 {
 9     return A>B;
10 }
11 int main()
12 {
13     int n;scanf("%d",&n);
14     int m;scanf("%d",&m);
15     for(int i=1;i<=n;i++)
16         scanf("%d",&a[i]);
17     sort(a+1,a+1+n,cmp);
18     int ans = 0;
19     for(int i=1;i<=n;i++)
20     {
21         m-=a[i];
22         ans++;
23         if(m<=0)break;
24     }
25     printf("%d\n",ans);
26 }

 

posted @ 2015-12-21 20:03  流年易逝  阅读(236)  评论(0编辑  收藏  举报