https://www.hackerrank.com/challenges/c-tutorial-conditional-if-else/problem
Conditional Statements | HackerRank
Practice using chained conditional statements.
www.hackerrank.com
Goal
Solution
You just need to create condtion statements for each condition like below.
// Conditional Statements
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
int main()
{
string n_temp;
getline(cin, n_temp);
int n = stoi(ltrim(rtrim(n_temp)));
// Write your code here
if(n > 9){
printf("Greater than 9");
}
else if(n == 1){
printf("one");
}
else if(n == 2){
printf("two");
}
else if(n == 3){
printf("three");
}
else if(n == 4){
printf("four");
}
else if(n == 5){
printf("five");
}
else if(n == 6){
printf("six");
}
else if(n == 7){
printf("seven");
}
else if(n == 8){
printf("eight");
}
else { // n == 9
printf("nine");
}
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
'software Engineering > (C++)HackerRank' 카테고리의 다른 글
[C++] HackerRank - Functions (0) | 2023.02.04 |
---|---|
[C++] HackerRank - For Loop (0) | 2023.01.24 |
[C++] HackerRank - Basic Data Types (0) | 2023.01.22 |
[C++] HackerRank - "Input and Output" (0) | 2023.01.18 |
[C++] HackerRank - "Say "Hello, World!" With C++" (0) | 2023.01.18 |