add discount

This commit is contained in:
juan 2021-10-12 19:23:17 +08:00
parent 8e1bb153e4
commit 38a17714e4
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7

39
pset3/5-discount.c Normal file
View file

@ -0,0 +1,39 @@
#include <stdio.h>
// Function prototype
void calculate(int i);
int main(void) {
// Initialize variables
int t; // Lines of input
int amount; // Amount of goods
int sum; // Sum of goods
int temp; // Temporary variable to store single price
scanf("%d", &t);
for (int i = 0; i < t; i++) {
sum = 0; // Sum should equal to 0 in each iteration
scanf("%d", &amount);
for (int j = 0; j < amount; j++) {
scanf("%d", &temp);
sum += temp;
}
calculate(sum);
}
return 0;
}
void calculate(int i) { // if cases and print discounted answer
if (i >= 400)
printf("%d\n", i - 160);
else if (i >= 300)
printf("%d\n", i - 110);
else if (i >= 200)
printf("%d\n", i - 70);
else if (i >= 100)
printf("%d\n", i - 30);
else
printf("%d\n", i);
}