본문 바로가기

Algorithm/Implementation

(C++, Rust) - 백준(BOJ) 23808 : 골뱅이 찍기 - ㅂ

반응형

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

 

23808번: 골뱅이 찍기 - ㅂ

서준이는 아빠로부터 골뱅이가 들어 있는 상자를 생일 선물로 받았다. 상자 안에는 ㅂ자 모양의 골뱅이가 들어있다. ㅂ자 모양은 가로 및 세로로 각각 5개의 셀로 구성되어 있다. 상자에는 정사

www.acmicpc.net

출력 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

기준점 n을 선언 후 입력받습니다.

📔 풀이과정

ㅂ을 출력하는데 있어서 두 가지 유형이 반복 출력되는 것을 볼 수 있습니다.

1. 가운데가 공백으로 빈 영역

2. 가운데가 차 있는 영역

이 부분을 출력하는 함수를 두 개 구현해줍니다.

📔 정답출력

형식에 맞게 구현한 두 함수를 적절히 호출해 출력합니다.


📕 Code

📔 C++

#include <bits/stdc++.h>
using namespace std;
int n;

void printOther(){
  for(int i = 0; i < n; i++){
    for(int j = 1; j <= n * 5; j++){
      if(j <= n || j > n * 4) cout << '@';
      else cout << ' ';
    }
    cout << '\n';
  }
}

void printSquare(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n * 5; j++){
      cout << '@';
    }
    cout << '\n';
  }
}

int main(){
  cin >> n;
  printOther();
  printOther();
  printSquare();
  printOther();
  printSquare();
}

 

📔 Rust

 

 

use std::io;


pub fn print_other(n: i64){
  for _ in 0..n {
    for j in 0..n*5 {
      if j < n || j >= n * 4 {
        print!("@");
      }
      else {
        print!(" ");
      }
    }
    println!();
  }
}

pub fn print_square(n: i64){
  for _ in 0..n {
    for _ in 0..n*5{
      print!("@");
    }
    println!();
  }
}

fn main(){
  let mut line = String::new();
  io::stdin().read_line(&mut line).expect("wrong io");
  let n = line.trim().parse::<i64>().unwrap();
  print_other(n);
  print_other(n);
  print_square(n);
  print_other(n);
  print_square(n);
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.