일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 파이썬 첼린지
- 운영체제
- 파이썬 알고리즘
- 파이썬
- 러스트 예제
- 자바 기초
- Database
- ubuntu
- 러스트
- java
- OS
- 데이터베이스
- 알고리즘
- Operating System
- 자바
- 오라클DB
- Python challenge
- 러스트 프로그래밍 공식 가이드
- 데이터 통신
- Reversing
- 오라클
- 파이썬 챌린지
- C
- 자바 개념
- Python
- 우분투
- data communication
- 백준 러스트
- Rust
- 백준
Archives
- Today
- Total
IT’s Portfolio
[Algorithm] Baekjoon - 2차원 배열 단계 본문
728x90
반응형
💻 Baekjoon Two Dimensional Array Stage
Matrix Sum
Basic Code
use std::io::{self, *};
fn make_matrix(n: u32, m: &str) -> (Vec<Vec<u32>>, Vec<Vec<u32>>) {
let mut v: Vec<Vec<Vec<u32>>> = Vec::new();
let mut iter = m.lines().skip(1);
for _ in 0..iter.clone().count() as u32/n {
let mut t: Vec<Vec<u32>> = Vec::new();
(0..n).for_each(|_| {
t.push(iter.next().unwrap().split_whitespace()
.map(|y| y.parse::<u32>().unwrap()).collect::<Vec<u32>>());
});
v.push(t);
}
(v[0].to_owned(), v[1].to_owned())
}
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let n = buf.lines().next().unwrap()
.split_whitespace().next().unwrap().parse::<u32>().unwrap();
if n == 0 {
println!("0");
return;
}
let (a, b) = make_matrix(n, buf.trim());
let mut r: Vec<Vec<u32>> = Vec::new();
for i in 0..n {
let mut t: Vec<u32> = Vec::new();
for j in 0..a[i as usize].clone().len() {
t.push(a[i as usize][j]+b[i as usize][j]);
}
r.push(t);
}
for i in r.iter() {
i.iter().for_each(|x| print!("{} ", x));
println!("");
}
}
Improvement Code
use std::io::{self, *};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut v: Vec<Vec<u32>> = Vec::new();
for l in buf.lines().skip(1) {
v.push(l.split(" ")
.map(|x| x.parse::<u32>().unwrap())
.collect::<Vec<u32>>());
}
for i in 0..v.len()/2 {
for j in 0..v[i].len() {
print!("{} ", v[i][j]+v[i+v.len()/2][j]);
}
println!("");
}
}
Matrix Sum
Basic Code
use std::io::{self, *};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut v: Vec<Vec<i32>> = Vec::new();
for l in buf.lines() {
v.push(
l.split_whitespace()
.map(|x| x.parse().unwrap())
.collect()
);
}
let mut max = -1;
let (mut a, mut b) = (0, 0);
for i in 0..9 {
for j in 0..9 {
if v[i][j] > max {
max = v[i][j];
(a, b) = (i+1, j+1);
}
}
}
println!("{max}");
println!("{a} {b}");
}
Vertical Reading
Basic Code
use std::io::{self, *};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut v: Vec<Vec<char>> = Vec::new();
let mut m = 0;
for l in buf.lines() {
v.push({
if l.chars().count() > m { m = l.chars().count(); }
l.chars().collect::<Vec<char>>()
});
}
for i in 0..m {
for j in 0..5 {
print!(
"{}",
match v[j].get(i) {
None => continue,
_ => v[j][i],
}
);
}
}
}
Colored Paper
Basic Code
use std::io::{self, *};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut p = [[0; 100]; 100];
let mut r = 0;
for l in buf.lines().skip(1) {
let (x, y) = {
let mut t = l.split(' ');
(t.next().unwrap().parse::<usize>().unwrap(),
t.next().unwrap().parse::<usize>().unwrap())
};
for i in x..x+10 {
for j in y..y+10 {
if p[i][j]==0 {
p[i][j]=1;
r+=1;
}
}
}
}
println!("{r}");
}
728x90
반응형
'Development Study > Rust' 카테고리의 다른 글
[Rust] Start Rust (Day 21) - An I/O Project: Building a Command Line Program (1) | 2023.08.24 |
---|---|
[Rust] Start Rust (Day 20) - Writing Automated Tests (0) | 2023.08.19 |
[Algorithm] Baekjoon - 심화 1 단계 (0) | 2023.08.03 |
[Algorithm] Baekjoon - 문자열 단계 (0) | 2023.07.31 |
[Algorithm] Baekjoon - 1차원 배열 단계 (0) | 2023.07.13 |
Comments