본문 바로가기

Algorithm/Implementation

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

반응형

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

 

23804번: 골뱅이 찍기 - ㄷ

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

www.acmicpc.net

간단 출력문제였습니다.

📕 풀이방법

📔 입력 및 초기화

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

📔 풀이과정

ㄷ에서 위와 아래의 선은 모양이 같습니다. 이외의 부분은 중앙이라고 생각했고 각 부분을 함수로 구현해 정답을 출력하게 했습니다.


📕 Code

C++

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

int n;

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

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

int main(){
  cin >> n;
  printEdge();
  printMid();
  printEdge();
}

Rust

use std::io;

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

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

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

  print_edge(n);
  print_mid(n);
  print_edge(n);
}

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