-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem01.c
More file actions
60 lines (51 loc) Β· 1.22 KB
/
Copy pathproblem01.c
File metadata and controls
60 lines (51 loc) Β· 1.22 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_SIZE 100
bool canBuyIceCream(int budget, int *iceCreamPrices, int count){
for (int i = 0; i < count; i++){
if (iceCreamPrices[i] == budget)
return true;
}
return false;
}
int main()
{
int budget;
int *iceCreamPrices = NULL;
int count = 0;
char line[MAX_SIZE];
printf("budget = ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &budget);
printf("iceCreamPrices = ");
while (1)
{
if (fgets(line, sizeof(line), stdin) == NULL)
break;
if (strcmp(line, "\n") == 0)
break;
int price;
if (sscanf(line, "%d", &price) == 1)
{
int *temp = realloc(iceCreamPrices, (count + 1) * sizeof(int));
if (temp == NULL)
{
free(iceCreamPrices);
return 1;
}
iceCreamPrices = temp;
iceCreamPrices[count++] = price;
}
}
if (canBuyIceCream(budget, iceCreamPrices, count))
{
printf("true\n");
}
else
{
printf("false\n");
}
return 0;
}