SDUTOJ 1750 - 汽车加油问题

#include
using namespace std;

int main()
{
    int n, k, dist[1005];
    cin>>n>>k;
    int temp = n;
    int count_num = 0;
    // dist数组记录从上一站到第i站的距离
    for (int i = 1; i <= k+1; i++){
        cin>>dist[i];
    }
    // 更新从第0站到第k+1站总的加油次数
    int i = 0;
    while(i <= k){
        // 如果从当前站加完油后仍然不足以到下一站,则该问题无解
        if (dist[i+1] > n){
            cout<<"No Solution!"<<endl;
            exit(0);
        }
        else if (temp-dist[i+1] >= 0){
             // 如果当前油量足以支撑走到下一站
             // 则更新当前油量
            temp = temp - dist[i+1];
            i++;
        }
        else{
            // 如果当前油量不足以支撑走到下一站
            // 则加满油,且更新加油次数
            temp = n;
            count_num++;
        }
    }
    cout<<count_num<<endl;
    return 0;
}

你可能感兴趣的:(SDUTOJ刷题,算法实验—贪心算法,c++,算法,贪心算法)