Functions | HackerRank
Learn how to write functions in C++. Create a function to find the maximum of the four numbers.
www.hackerrank.com
Goal
Get four arguments and return the greatest of the four integers
Solution
Because you just need to return the greatest integer.
You can compare a and b first, then c and d.
If you find already greateest integer, you can simply return the value.
// Functions
#include <iostream>
#include <cstdio>
using namespace std;
/*
Add `int max_of_four(int a, int b, int c, int d)` here.
*/
int max_of_four(int a, int b, int c, int d){
if(a >= b){
if (a >= c){
if (a >= d){
return a;
}
else{
return d;
}
}
else{
if( c>=d){
return c;
}
else{
return d;
}
}
}
else{
if (b >= c){
if (b >= d){
return b;
}
else {
return d;
}
}
else{
if (c >= d){
return c;
}
else {
return d;
}
}
}
return 0;
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
'software Engineering > (C++)HackerRank' 카테고리의 다른 글
[C++] HackerRank - Arrays Introduction (0) | 2023.02.09 |
---|---|
[C++] HackerRank - Pointer (0) | 2023.02.08 |
[C++] HackerRank - For Loop (0) | 2023.01.24 |
[C++] HackerRank - Conditional Statements (0) | 2023.01.24 |
[C++] HackerRank - Basic Data Types (0) | 2023.01.22 |