-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappy_party_train.c
68 lines (52 loc) · 1.57 KB
/
happy_party_train.c
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
67
#include <stdio.h>
#include <stdlib.h>
struct Car {
int passenger;
int flammable;
struct Car *next;
};
void attachCar(struct Car *head, int passenger, int flammable) {
struct Car *curr = head;
while(curr->next) curr = curr->next; // Find the end of the train
struct Car *newcar = malloc(sizeof(struct Car)); // Make a new car
newcar->passenger = passenger; // Let passengers in
newcar->flammable = flammable; // Load flammable items
newcar->next = NULL; // This is the last car
curr->next = newcar; // Attach new car to the train
}
int fire(struct Car *head);
int main(int argc, char *argv[])
{
int cars;
int human[13], moeru[13];
struct Car head;
head.next = NULL; // passenger & flammable for head in useless
scanf("%d", &cars);
for (int i = 0; i < cars; ++i) {
scanf("%d", &human[i]);
}
for (int i = 0; i < cars; ++i) {
scanf("%d", &moeru[i]);
}
for (int i = 0; i < cars; ++i) {
attachCar(&head, human[i], moeru[i]);
}
printf("%d", fire(&head)); // The train is on fire now
return 0;
}
// Your code goes here
int fire(struct Car* head) {
struct Car* p=head->next;
int returnVal=0;
int flammable,passenger;
while(p->flammable>p->passenger){
returnVal++;
flammable=p->flammable;
passenger=p->passenger;
p=p->next;
if(p==NULL)
break;
(p->passenger)+=passenger;
}
return returnVal;
}