C++旅馆问题。

有总钱数

有每房每天住需要多少钱

问最少可以住几天

最后输入的是钱数。前边输入没个住所每天多少钱

例如:

1001 1002 1003 1004 1000

-1

100 500 600

最少一天,最多2天

写了一个程序但是估计还是考虑不周只有0.5的通过率

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 int main(){
 6 int x;
 7 vector<int> vec;
 8 int sum=0;
 9 
10 while(cin>>x){
11  vec.push_back(x);
12 }
13 sum=vec.back();
14 vec.pop_back();
15 vector<int>::iterator it;
16 int flage=0;
17 for(it=vec.begin();it!=vec.end();it++)
18 {
19     if(*it>sum)
20     {
21         flage=0;
22     }
23     else{
24     flage=1;
25     }
26 }
27 if(flage==0)
28 {
29     cout<<-1<<endl;
30 
31 }
32 else{
33    sort(vec.begin(),vec.end(),greater<int>());
34    int count=0;
35    for(it=vec.begin();it!=vec.end();it++){
36    if(*it<=sum&&sum>0){
37    count+=sum/(*it);
38    sum=sum%(*it);
39    }
40    }
41    cout<<count<<endl;
42 }
43 return 0;
44 }


可能对这个最多最少还是不太理解。。。。。。。。。。。。。。。。。。。

 

posted on 2016-09-27 12:14  邗影  阅读(466)  评论(0编辑  收藏  举报

导航