+ 2
You are working on a ticketing system. A ticket costs $10. The office is running a discount campaign: each group of 5 people is
#include <iostream> using namespace std; int main() { int l,a[5]; for (int i = 0; i < 5; ++i) { cin >> a[i]; } //your code goes here if(a[0] < a[1] && a[0] < a[2] && a[0] < a[3] && a[0] < a[4]){ l=a[0]; } if(a[1] < a[0] && a[1] < a[2] && a[1] < a[3] && a[1] < a[4]){ l=a[1];} else if(a[2] < a[0] && a[2] < a[1] && a[2] < a[3] && a[2] < a[4]){ l=a[2];} else if(a[3] < a[0] && a[3] < a[2] && a[3] < a[1] && a[3] < a[4]){ l=a[3];} else if(a[4] < a[0] && a[4] < a[1] && a[4] < a[2] && a[4] < a[3]){ l=a[4];} float m=(float)(l*50)/100; float j=50-m; cout<<j; return 0; }
2 Antworten
+ 2
Save the first element of the array into a variable as the youngest person, then loop over the remaining people and compare to the current youngest variable. If the next element in the array is younger update the variable to that element if not move on to the next element in the array for comparison. Repeat until the last element of the array. This will simplify and reduce your code and get the smallest value in the array.
Now you can do the math to get the cost.
0
#include <iostream>
#include <string>
#include <climits>
using namespace std;
int main() {
int ages[5];
double min = INT_MAX;
double total;
double discount;
for (int i = 0; i < 5; i++) {
cin >> ages[i];
if (ages[i] < min)
min = ages[i];
}
total = 50 * (min/100);
discount = 50 - total;
cout << discount;
cout << endl;
return 0;
}