7-7 汽车加油问题 (20 分)
题目来源:王晓东《算法设计与分析》
一辆汽车加满油后可行驶 n公里。旅途中有若干个加油站。设计一个有效算法,指出应 在哪些加油站停靠加油,使沿途加油次数最少。
输入格式:
第一行有 2 个正整数n和 k(k<=1000 ),表示汽车加满油后可行驶n公里,且旅途中有 k个加油站。 第二行有 k+1 个整数,表示第 k 个加油站与第k-1 个加油站之间的距离。 第 0 个加油站表示出发地,汽车已加满油。 第 k+1 个加油站表示目的地。
输出格式:
输出最少加油次数。如果无法到达目的地,则输出“No Solution!”。
输入样例:
7 7
1 2 3 4 5 1 6 6
输出样例:
4
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <string>
#include <string.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int n,k;
cin>>n>>k;
int a[k+1];
for(int i=0;i<k+1;i++){
cin>>a[i];
}
int res = 0;
int now = n;
int size = 0;
bool flag = true;
for(int i=0;i<k+1;i++){
if(now >= a[i]){
res++;
now -= a[i];
}