zoukankan      html  css  js  c++  java
  • 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 }


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

  • 相关阅读:
    Power of Cryptography
    Radar Installation
    Emag eht htiw Em Pleh
    Help Me with the Game
    89. Gray Code
    87. Scramble String
    86. Partition List
    85. Maximal Rectangle
    84. Largest Rectangle in Histogram
    82. Remove Duplicates from Sorted List II
  • 原文地址:https://www.cnblogs.com/8335IT/p/5912352.html
Copyright © 2011-2022 走看看