-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.cpp
67 lines (66 loc) · 1.52 KB
/
car.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*************************************************************************
> File Name: car.cpp
> Author:
> Mail:
> Created Time: 2017年04月10日 星期一 16时59分20秒
************************************************************************/
//汽车加油问题
//输入加满油后可行驶公里数和加油站数,且输入每个加油站之间的距离
//ex:7 7
// 1 2 3 4 5 1 6 6
//目的地:0 1 3 6 10 15 16 22 28终点(上下对应)
#include<iostream>
#include<fstream>
using namespace std;
#define Max 100
static int num = 0;
void Car(int aa[],int nn,int kk)
{
int i,s;
for(i = 0; i <= kk; i++)
{
if(aa[i] > nn)
{
cout << "走不到终点" << endl;
return;
}
}
for(i = 0,s = 0; i <= kk; i++)
{
s += aa[i];
if(s > nn)
{
num++;
s = aa[i];
}
}
}
int main()
{
int i,n,k;
int a[Max] = {0};
ifstream in("text1.txt");
if(in)
{
cout << "请输入一次加满油后行驶距离和加油站数:" <<endl;
in >> n >> k;
cout << n <<' ' << k << endl;
cout << "请输入相邻2个加油站之间的距离:" <<endl;
for(i = 0; i < k + 1; i++)
{
in >> a[i];
cout << a[i] << ' ';
}
cout <<endl;
in.close();
}
Car(a,n,k);
ofstream out("text.txt");
if(out)
{
out << num;
out.close();
}
cout << "加油次数最少为:"<< num << endl;
return 0;
}