Inherited Code / Medium
문제 링크
https://www.hackerrank.com/challenges/inherited-code/problem
Inherited Code | HackerRank
Inherited Code | HackerRank We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
www.hackerrank.com
Exception을 정의하는 문제다.
입력받은 username의 길이가 5미만인 경우에 대한 exception을 정의하면 된다.
C++ 풀이
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
using namespace std;
/* Define the exception here */
class BadLengthException {
private:
int n;
public:
BadLengthException(int N) {
n = N;
}
int what() {
return n;
}
};
bool checkUsername(string username) {
bool isValid = true;
int n = username.length();
if (n < 5) {
throw BadLengthException(n);
}
for (int i = 0; i < n - 1; i++) {
if (username[i] == 'w' && username[i + 1] == 'w') {
isValid = false;
}
}
return isValid;
}
int main() {
int T; cin >> T;
while (T--) {
string username;
cin >> username;
try {
bool isValid = checkUsername(username);
if (isValid) {
cout << "Valid" << '\n';
}
else {
cout << "Invalid" << '\n';
}
}
catch (BadLengthException e) {
cout << "Too short: " << e.what() << '\n';
}
}
return 0;
}
반응형
'알고리즘 · 코딩' 카테고리의 다른 글
[HackerRank] Exceptional Server (0) | 2022.03.08 |
---|---|
[LeetCode] Two Sum (0) | 2022.02.25 |
[SWEA 1954] 달팽이 숫자 (0) | 2022.02.13 |
[프로그래머스] n^2 배열 자르기 (0) | 2021.12.23 |
[프로그래머스] 전력망을 둘로 나누기 (0) | 2021.12.22 |