일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 파이썬
- Python challenge
- OS
- Python
- 러스트
- 오라클DB
- Rust
- 오라클
- 파이썬 첼린지
- 알고리즘
- Database
- 백준
- 파이썬 챌린지
- 우분투
- ubuntu
- java
- 데이터베이스
- rust example
- 러스트 프로그래밍 공식 가이드
- 파이썬 알고리즘
- 자바
- 백준 러스트
- data communication
- 데이터 통신
- 자바 개념
- 자바 기초
- Operating System
- Reversing
- 운영체제
- 러스트 예제
Archives
- Today
- Total
IT’s Portfolio
[Algorithm] Baekjoon - 1차원 배열 단계 본문
728x90
반응형
💻 Baekjoon One Dimensional Array Stage
Count Number
Basic Code
use std::io;
fn input() -> String {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.pop();
buf
}
fn main() {
let mut n: Vec<i32> = Vec::new();
let mut v = 0;
(0..3).for_each(
|x| {
let s = input();
if x==1 {
n = s.split(" ").map(
|y| y.parse().unwrap()
).collect();
} else if x==2 {
v = s.parse().unwrap();
}
}
);
println!("{}", count_number(n, v));
}
fn count_number(l: Vec<i32>, v: i32) -> i32 {
let mut cnt = 0;
for i in l {
if i==v {
cnt+=1;
}
}
cnt
}
Improvement Code - flat_map & filter
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut n = buf.split_ascii_whitespace().flat_map( str::parse::<i32> );
let (_, v) = (n.next(), n.next_back().unwrap());
let cnt = n.filter(|&x| x==v).count();
println!("{cnt}");
}
trait_Iterator_method_flat_map in Official Document
trait_Iterator_method_filter in Official Document
Number less than X
Basic Code
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut n = buf.split_ascii_whitespace().skip(1)
.flat_map( str::parse::<i32> );
let x = n.next();
n.filter(|&i| Some(i) < x).for_each(
|i| print!("{i} ")
);
}
Min, Max
Basic Code
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
let mut v = Vec::new();
io::stdin().read_to_string(&mut buf).unwrap();
{
let n = buf.split_ascii_whitespace().skip(1)
.flat_map( str::parse::<i32> );
n.for_each(|x| v.push(x));
}
let mut max = &v[0];
let mut min = &v[0];
v.iter().for_each(
|x| {
if max < x { max = x; }
if min > x { min = x; }
}
);
println!("{min} {max}");
}
Improvement Code
use std::io::{self, Read};
fn main() {
const MAX: i32 = 1_000_000;
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let n = buf.split_ascii_whitespace().skip(1)
.flat_map( str::parse::<i32> );
let (min, max) = n.fold(
(MAX, -MAX), |(min, max), num| {
(if num < min { num } else { min },
if num > max { num } else { max })
});
// let (min, max) = n.fold(
// (MAX, -MAX), |(min, max), num| (num.min(min), num.max(max))
// );
// 위와 같은 코드인데 min과 max 메서드를 사용하여 간결하게 짤 수 있음
println!("{min} {max}");
}
trait_Iterator_method_fold in Official Document
Max Value
Basic Code
use std::io::{self, Read};
fn main() {
const MAX: i32 = 100;
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let n = buf.split('\n').flat_map( str::parse::<i32> );
let (c, max) = n.enumerate().fold(
(0, -MAX), |(c, max), (i , v)| {
if max > v { (c, max) }
else { (i+1, v.max(max)) }
});
println!("{max}\n{c}");
}
Improvement Code - max_by & cmp
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let n = buf.split_ascii_whitespace().flat_map( str::parse::<i32> );
let max = n.enumerate().max_by(
|x, y| (x.1.cmp(&y.1))
).unwrap();
println!("{}\n{}", max.1, max.0+1);
}
trait_Iterator_method_max_by in Official Document
trait_Ord_method_cmp in Official Document
Put the Ball
Basic Code
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let n;
{
let v: Vec<i32> = buf.lines().next().unwrap().split(' ').map(
|x| x.parse().unwrap()
).collect();
n = v[0];
}
let mut basket = vec![0; n as usize];
for l in buf.lines().skip(1) {
let mut tmp = l.split(' ').flat_map( str::parse::<i32> );
let (i, j, k) = (
tmp.next().unwrap()-1,
tmp.next().unwrap()-1,
tmp.next().unwrap()
);
(i..=j).for_each(|x| {
basket[x as usize] = k;
});
}
basket.iter().for_each(|x| print!("{x} "));
}
Change the Ball
Basic Code
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let n;
let mut b: Vec<i32> = Vec::new();
{
let v: Vec<i32> = buf.lines().next().unwrap().split(' ')
.map(|x| x.parse().unwrap()).collect();
n = v[0];
}
(0..n).for_each(|x| b.push(x+1));
for l in buf.lines().skip(1) {
let mut t = l.split(' ').flat_map(str::parse::<i32>);
let (i, j) = (t.next().unwrap()-1, t.next().unwrap()-1);
b.swap(i as usize, j as usize);
// let tmp = b[i as usize];
// b[i as usize] = b[j as usize];
// b[j as usize] = tmp;
// 해당 코드와 같은 동작을 하는 벡터의 swap 메서드
}
b.iter().for_each(|x| print!("{x} "));
}
Who hasn't submitted an assignment?
Basic Code
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut iter = buf.split('\n').flat_map(str::parse::<u8>);
let sub = [0; 28].map(|_| iter.next().unwrap());
(1..=30).for_each(|x| if !sub.contains(&x) { println!("{x}") });
}
Improvement Code
use std::io::{stdin, BufReader, BufRead};
fn main() {
let s: Vec<u8> = BufReader::new(stdin()).lines()
.map(|x| x.unwrap().parse::<u8>().unwrap()).collect();
(1..=30).for_each(|i| if !s.contains(&i) { println!("{i}") });
}
struct_BufReader in Official Document
trait_BufRead in Official Document
Modulo
Basic Code
use std::io::{stdin, BufReader, BufRead};
fn main() {
let mut buf: Vec<u32> = BufReader::new(stdin()).lines()
.map(|x| x.unwrap().parse::<u32>().unwrap()%42).collect();
buf.sort();
buf.dedup();
println!("{}", buf.len());
}
Reverse the Basket
Basic Code - swap
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let n: u32;
{
let mut it = buf.lines().next().unwrap().split(' ')
.map(|x| x.parse().unwrap());
n = it.next().unwrap();
}
let mut b: Vec<_> = Vec::new();
(1..=n).for_each(|x| b.push(x));
for l in buf.lines().skip(1) {
let mut it = l.split(' ').flat_map(str::parse::<u32>);
let (i, j) = (it.next().unwrap()-1, it.next().unwrap()-1);
(0..=(j-i)/2).for_each(
|t| b.swap((t+i) as usize, (j-t) as usize)
);
}
b.iter().for_each(|i| print!("{i} "));
}
Basic Code - reverse
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let n: usize;
{
let mut it = buf.lines().next().unwrap().split(' ')
.map(|x| x.parse().unwrap());
n = it.next().unwrap();
}
let mut b: Vec<_> = Vec::new();
(1..=n).for_each(|x| b.push(x));
for l in buf.lines().skip(1) {
let mut it = l.split(' ').flat_map(str::parse::<usize>);
let (i, j) = (it.next().unwrap()-1, it.next().unwrap()-1);
b[i..=j].reverse();
}
b.iter().for_each(|i| print!("{i} "));
}
Average
Basic Code
use std::io::{self, Read};
fn main() {
const MAX: f64 = 100.0;
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let s: Vec<f64> = buf.lines().skip(1).next().unwrap()
.split(' ').map(|x| x.parse().unwrap()).collect();
let max = s.iter().fold(
-MAX, |max, n| n.max(max));
let mut r: Vec<f64> = Vec::new();
s.iter().for_each(|x| r.push(x/max*100.0));
println!("{}", r.iter().sum::<f64>()/r.len() as f64);
}
728x90
반응형
'Development Study > Rust' 카테고리의 다른 글
[Algorithm] Baekjoon - 심화 1 단계 (0) | 2023.08.03 |
---|---|
[Algorithm] Baekjoon - 문자열 단계 (0) | 2023.07.31 |
[Rust] Start Rust (Day 19) - Generic Types, Traits, and Lifetimes (1) | 2023.07.01 |
[Algorithm] Baekjoon - 반복문 단계 (0) | 2023.05.24 |
[Algorithm] Baekjoon - 조건문 단계 (0) | 2023.04.04 |
Comments