※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 26516번 문제인 Mutint이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/26516 

 

26516번: Mutint

Several integers, each on one line. The end of input is signaled with a zero on the last line. All integers, except the last integer, are positive.

www.acmicpc.net

먼저 문자열을 구성하는 가장 큰 문자 중 가장 왼쪽에 있는 문자를 문제에서 요구하는 대로 수정해주자. 이 수정된 문자열에서 leading zero를 뺀 문자열을 출력해 문제를 해결할 수 있다.

 

출력을 해야하는 문자열이 나타내는 정수가 0인 테스트케이스는 입력으로 주어지지 않으므로(assert문으로 확인) 안심하고 제출하자.

 

아래는 제출한 소스코드이다.

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

string s; int slen;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> s;
	while (s != "0") {
		slen = s.length();
		char mx = '0' - 1; int mxidx = -1;
		for (int i = 0; i < slen; i++) {
			if (s[i] > mx) mx = s[i], mxidx = i;
		}

		if ((s[mxidx] - '0') & 1) s[mxidx] = '0';
		else s[mxidx] = (s[mxidx] - '0' + 4) % 10 + '0';

		int idx = 0;
		while (idx < slen && s[idx] == '0') idx++;

		if (idx == slen) cout << 0 << '\n';
		else cout << s.substr(idx, slen - idx) << '\n';

		cin >> s;
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 5346 // C++] Frodo Sequence  (0) 2022.12.23
[BOJ 26711 // C++] A+B  (0) 2022.12.23
[BOJ 20112 // C++] 사토르 마방진  (0) 2022.12.23
[BOJ 17828 // C++] 문자열 화폐  (0) 2022.12.23
[BOJ 26565 // C++] Time Limits  (0) 2022.12.23

+ Recent posts