by CodeChum Admin
We've already tried comparing 3 numbers to see the largest among all, so let's try a more complicated mission where we locate where among the 5-digit integer is the largest one. There are only 4 possible cases for this mission:
Now, show me how far you've understood your lessons!
Input
A line containing a five-digit integer.
14632
Output
A line containing a string.
Middle
#include <iostream>
using namespace std;
int main() {
int n;
int d, max_d, i, i_max;
cin >> n;
d = n % 10;
n /= 10;
i = 1;
max_d = d;
i_max = i;
while (i < 5) {
i++;
d = n % 10;
n /= 10;
if (d > max_d) {
max_d = d;
i_max = i;
}
}
if (i_max == 5) {
cout << "Leftmost";
}
else if (i_max == 3) {
cout << "Middle";
}
else if (i_max == 1) {
cout << "Rightmost";
}
else {
cout << "Unknown";
}
return 0;
}
Comments
Leave a comment