본문 바로가기

Algorithm/Implementation

(C++, Rust) - 백준(BOJ) 23794 : 골뱅이 찍기 - 정사각형

반응형

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

 

23794번: 골뱅이 찍기 - 정사각형

첫째 줄부터 $N+2$번째 줄까지 차례대로 골뱅이를 출력한다.

www.acmicpc.net

간단 출력 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

n을 입력받습니다.

📔 정답출력

조건대로 '@'를 출력합니다.


📕 Code

C++

#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
  cin >> n;
  for(int i = 0; i < n + 2; i++) cout << '@';
  cout << '\n';
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n + 2; j++){
      if(j == 0 || j == n + 1) cout << '@';
      else cout << ' ';
    }
    cout << '\n';
  }
  for(int i = 0; i < n + 2; i++) cout << '@';
}

Rust

use std::io;

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

  for _ in 0..n+2 {
    print!("@");
  }
  println!("");

  for _ in 0..n {
    for j in 0..n+2{
      if j == 0 || j == n + 1 {
        print!("@");
      }
      else {
        print!(" ");
      }
    }
    println!("");
  }

  for _ in 0..n+2 {
    print!("@");
  }
}

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