피너클의 it공부방
백준 1346 유진수 (c++) : 피너클 본문
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
반응형
'백준' 카테고리의 다른 글
백준 1817 짐 챙기는 숌 (c++) : 피너클 (0) | 2022.07.16 |
---|---|
백준 1531 투명 (c++) : 피너클 (0) | 2022.07.14 |
백준 13422 도둑 (c++) : 피너클 (0) | 2022.07.12 |
백준 12932 노래방 (c++) : 피너클 (0) | 2022.07.08 |
백준 3047 ABC (c++) : 피너클 (0) | 2022.07.07 |
Comments