Algorithm/📖Baekjoon

#10250 ACM 호텔

yewoneeee 2022. 5. 7. 22:55

# 문제

# 입력 및 출력

# 풀이

#include <iostream>
#include <string>
using namespace std;

int main() {
	int t, h, w, n;
	cin >> t;
	while (t--) {
		cin >> h >> w >> n;
		string yy = "", xx = "";
		int x, y;
		y = n % h;
		x = n / h;
		if (y == 0) {
			yy += to_string(h);
			if (x < 10) xx += "0";
			xx += to_string(x);
		}
		else {
			yy += to_string(y);
			if (x + 1 < 10) xx += "0";
			xx += to_string(x + 1);
		}
		cout << yy << xx << endl;
	}
}

복잡하게 풀었는데 아래처럼 간단하게 해결할 수 있음

#include <iostream>
using namespace std;

int main() {
	int t, h, w, n;
	cin >> t;
	while (t--) {
		cin >> h >> w >> n;
		int x = 1;
		while (n > h) {
			n -= h;
			x++;
		}
		cout << n * 100 + x << "\n";
	}
}

 

굳이 string으로 출력할 필요없이 100을 곱해주는 것으로 해결할 수 있음

또한 n>h까지 반복이므로 n==h가 되는 경우엔 반복문이 실행되지 않기 때문에 n값이 0이 되지 않음