Notice
Recent Posts
Recent Comments
Link
250x250
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

피너클의 it공부방

백준 1346 유진수 (c++) : 피너클 본문

백준

백준 1346 유진수 (c++) : 피너클

피너클 2022. 7. 13. 14:24
728x90
반응형

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

 

1356번: 유진수

첫째 줄에 수 N이 주어진다. 이 수는 2,147,483,647보다 작거나 같은 자연수이다.

www.acmicpc.net

문자열 문제다.

int string_to_int(string a) {
	int num = 1;
	for (int i = 0; i < a.length(); i++) num *= (a[i] - '0');
	return num;
}

먼저 함수를 하나 만든다. string을 전달받고 string안의 숫자들을 전부 곱해 리턴하는 함수다.

string str;
cin >> str;
if (str.length() == 1) {
	cout << "NO" << endl;
	return 0;
}

main에서 문자열을 입력받는다. 만약 문자열의 길이가 1이라면 무조건 유진수가 아니니 NO를 출력한다.

bool chk = false;
for (int i = 1; i < str.length(); i++) {
	string a = str.substr(0, i);
	string b = str.substr(i);
	
	if (string_to_int(a) == string_to_int(b)) {
		chk = true;
		break;
	}
}

그후 str의 부분 문자열을 하나하나 확인해본다.

string a = str.substr(0, i)는 str의 0번째 부터 시작해 i - 1번째 까지의 부분 문자열을 a에 집어넣는 코드다.

string b = str.substr(i)는 str의 i번째 부터 마지막 까지의 부분 문자열을 b에 집어넣는 코드다.

이제 만약 a와 b의 곱이 같다면 chk를 true로 하고 반복문을 빠져나온다.

if (chk) cout << "YES" << endl;
else cout << "NO" << endl;

만약 chk가 true라면 YES를 출력하고 아니면 NO를 출력한다.

#include <iostream>
#include <string>

using namespace std;

int string_to_int(string a) {
	int num = 1;
	for (int i = 0; i < a.length(); i++) num *= (a[i] - '0');
	return num;
}

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

	string str;
	cin >> str;
	if (str.length() == 1) {
		cout << "NO" << endl;
		return 0;
	}

	bool chk = false;
	for (int i = 1; i < str.length(); i++) {
		string a = str.substr(0, i);
		string b = str.substr(i);
		
		if (string_to_int(a) == string_to_int(b)) {
			chk = true;
			break;
		}
	}
	if (chk) cout << "YES" << endl;
	else cout << "NO" << endl;
}

전체코드다.

728x90
반응형
Comments