본문 바로가기

알고리즘 · 코딩

[HackerRank] Exceptional Server

Exceptional Server / Medium

 

문제링크

https://www.hackerrank.com/challenges/exceptional-server/problem

 

Exceptional Server | HackerRank

Handle server errors.

www.hackerrank.com

여러가지 케이스에 대한 exception 처리를 간단하게 구현하는 문제이다.


C++ 풀이

#include <iostream>
#include <exception>
#include <string>
#include <stdexcept>
#include <vector>
#include <cmath>
using namespace std;

class Server {
private:
	static int load;
public:
	static int compute(long long A, long long B) {
		load += 1;
		if (A < 0) {
			throw std::invalid_argument("A is negative");
		}
		vector<int> v(A, 0);
		int real = -1, cmplx = sqrt(-1);
		if (B == 0) throw 0;
		real = (A / B) * real;
		int ans = v.at(B);
		return real + A - B * ans;
	}
	static int getLoad() {
		return load;
	}
};
int Server::load = 0;

int main() {
	int T; cin >> T;
	while (T--) {
		long long A, B;
		cin >> A >> B;

		/* Enter your code here. */
		try {
			cout << Server::compute(A, B) << endl;
		}
		catch (bad_alloc& error) {
			cout << "Not enough memory" << endl;
		}
		catch (exception& error) {
			cout << "Exception: " << error.what() << endl;
		}
		catch (...) {
			cout << "Other Exception" << endl;
		}

	}
	cout << Server::getLoad() << endl;
	return 0;
}
LIST

'알고리즘 · 코딩' 카테고리의 다른 글

[HackerRank] Inherited Code  (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