본문 바로가기

Algorithm/Implementation

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

반응형

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

 

23811번: 골뱅이 찍기 - ㅌ

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

www.acmicpc.net

출력문제였습니다.

📕 풀이방법

📔 입력 및 초기화

n선언 후 입력받습니다.

📔 풀이과정

다음 두 부분이 출력예시에서 반복되는 것을 알 수 있습니다.

1. 길쭉한 부분

2. 짧은 부분

이를 함수로 구현해줍니다.

📔 정답출력

형식에 맞게 출력합니다.


📕 Code

📔 C++

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

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

void printShort(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cout << "@";
    }
    cout << '\n';
  }
}

int main(){
  cin >> n;
  printLong();
  printShort();
  printLong();
  printShort();
  printLong();
}

 

📔 Rust

use std::io;

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

pub fn print_short(n: i64){
  for _ in 0..n {
    for _ in 0..n {
      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_long(n);
  print_short(n);
  print_long(n);
  print_short(n);
  print_long(n);
}

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