728x90
앞에서 했던 실수들을 생각하며 차근차근 그리고 빠르게 풀어나갔다. (10분)
my full code
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
int N;
cin >> N;
for (int i = 1; i <= N; i++) {
bool clap = false;
string num = to_string(i);
for (auto n : num) {
if ((n - '0') % 3 == 0 && (n - '0') != 0) {
clap = true;
cout << '-';
}
}
(!clap) ? cout << num << " " : cout << " ";
}
}
문제
https://swexpertacademy.com/main/code/problem/problemDetail.do
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
728x90
[접근1]
숫자 하나씩 string으로 만들어서 한자리수씩 확인한다.
3, 6, 9인지는 %3이 0인지 확인하면 된다. 0일때는 따로확인해준다.
만약 3, 6, 9가 들어있다면, 각자리를 확인하는 동시에 '-'를 출력해주고, 3, 6, 9가 들어있는지 확인하는 bool을 true로 바꿔준다.
string num = to_string(i);
for (auto n : num) {
if ((n - '0') % 3 == 0 && (n - '0') != 0) {
clap = true;
cout << '-';
}
}
[접근2]
3, 6, 9가 아니면 숫자 그대로 출력, 3, 6, 9가 들어있었으면 공백 하나만 출력한다.
(!clap) ? cout << num << " " : cout << " ";728x90
'coding > algorithm' 카테고리의 다른 글
| [programmers] Lv.3 네트워크(c++) (0) | 2022.05.25 |
|---|---|
| [programmers] Lv.2 타겟 넘버(c++) (0) | 2022.05.24 |
| [SW Expert Academy] D2 1954. 달팽이 숫자 (0) | 2022.05.24 |
| [SW Expert Academy] D3 1208. Flatten (0) | 2022.05.23 |
| [백준/baekjoon] Silver3 2606. 바이러스 (0) | 2022.05.23 |
댓글