码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2487

时间:2015-06-11 12:22:46      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<iostream>
 2 #include<stdio.h>
 3 using namespace std;
 4 
 5 int compare(void const * i,void const * j);
 6 int main()
 7 {
 8     //freopen("acm.acm","r",stdin);
 9     int num;
10     int i;
11     int j;
12     int st;
13     int sum;
14     int f_num;
15     int * a;
16     cin>>num;
17     j = 1;
18     while(num --)
19     {
20         cin>>st;
21         cin>>f_num;
22         sum = 0;
23         a = new int[f_num];
24         for(i = 0; i < f_num; ++ i)
25         {
26             cin>>a[i];
27         }
28         qsort(a,f_num,sizeof(int),compare);
29         for(i = 0; i < f_num; ++ i)
30         {
31             sum += a[i];
32             if(sum >= st)
33             {
34                 cout<<"Scenario #"<<j<<":"<<endl;
35                 cout<<i+1<<endl;
36                 ++j;
37                 cout<<endl;
38                 break;
39             }
40         }
41         if(i == f_num)
42         {
43             cout<<"Scenario #"<<j<<":"<<endl;
44             cout<<"impossible"<<endl;
45             ++ j;
46             cout<<endl;
47         }
48         delete[] a;
49         
50     }
51 }
52 int compare(void const * i,void const * j)
53 {
54     return *((int *)j) - *((int *)i); 
55 }

 

POJ 2487

标签:

原文地址:http://www.cnblogs.com/gavinsp/p/4568437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!