일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 러스트 프로그래밍 공식 가이드
- 오라클DB
- 파이썬 첼린지
- 백준 러스트
- 파이썬 알고리즘
- ubuntu
- 운영체제
- 자바
- C
- 자바 개념
- 러스트 예제
- Database
- java
- Reversing
- 오라클
- Python
- 데이터 통신
- 백준
- data communication
- 알고리즘
- Rust
- Python challenge
- 파이썬
- 우분투
- 자바 기초
- OS
- 데이터베이스
- 러스트
- Operating System
- 파이썬 챌린지
Archives
- Today
- Total
IT’s Portfolio
[Algorithm] Baekjoon - 문자열 단계 본문
728x90
반응형
💻 Baekjoon String Stage
Character & String
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.lines();
let s = String::from(iter.next().unwrap());
let i = iter.next().unwrap().parse::<usize>().unwrap();
println!("{}", &s[i-1..i]);
}
Measure Word Length
Basic Code
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
println!("{}", buf.trim().len());
}
String
Basic Code
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
for l in buf.lines().skip(1) {
println!("{}{}", &l[..1], &l[l.len()-1..]);
}
}
ASCII Code
Basic Code
use std::io::{self, Read};
fn main() {
let mut b = [0; 1];
io::stdin().read(&mut b).unwrap();
println!("{}", &b[0]);
}
Sum of Numbers
Basic Code
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut v: Vec<i32> = Vec::new();
let mut iter = buf.lines();
let (n, m) = (iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap());
(1..=n).for_each(|x| {
let tmp = &m[x-1..x];
v.push(tmp.parse::<i32>().clone().unwrap());
});
println!("{}", v.iter().sum::<i32>());
}
Improvement Code - chars & to_digit
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
for l in buf.lines().skip(1) {
let s = l.chars().map(|x| x.to_digit(10).unwrap())
.sum::<u32>();
println!("{s}");
}
}
Find the Alphabet
Basic Code
use std::io::{self, *};
fn main() {
let mut l = [-1_i32; 26];
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
for (i, v) in buf.trim().bytes().enumerate() {
if l[(v-97) as usize] == -1 {
l[(v-97) as usize] = i as i32;
}
}
l.iter().for_each(|x| print!("{x} "));
}
Repeat String
Basic Code
use std::io::{self, *};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
for l in buf.lines().skip(1) {
let mut sp = l.split(' ');
let (r, s) = (
sp.next().unwrap().parse::<u8>().unwrap(),
sp.next().unwrap().chars());
s.for_each(|x| (0..r).for_each(|_| print!("{x}")));
println!("");
}
}
Number of words
Basic Code - split()
use std::io::{self, *};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
match buf.trim().len() {
0 => println!("0"),
_ => println!("{}", buf.trim().split(' ').count()),
}
}
Basic Code - split_whitespace()
use std::io::{self, *};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
println!("{}", buf.split_whitespace().count());
}
상수
Basic Code
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let m: i32 = buf.split_whitespace()
.map(|x| x.chars().rev().collect::<String>())
.map(|x| x.parse().unwrap()).max().unwrap();
println!("{m}");
}
Dial
Basic Code
use std::{io, collections::HashMap};
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let mut h: HashMap<u8, Vec<u8>> = HashMap::new();
let mut t = 65;
let mut r = 0;
(3..=10).for_each(|x| {
let mut t2: Vec<u8> = Vec::new();
if x!=8 && x!=10 {
(0..3).for_each(|_| {t2.push(t); t+=1;});
h.insert(x, t2);
} else {
(0..4).for_each(|_| {t2.push(t); t+=1;});
h.insert(x, t2);
}
});
for i in buf.trim().bytes() {
for (k, v) in h.iter() {
if v.contains(&i) {
r+=k;
}
}
}
println!("{r}");
}
Improvement Code
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let r = buf.trim().bytes().map(
|x| {
match x-65 {
0..=17 => (x-65)/3+3,
18 => 8,
19..=21 => 9,
22..=25 => 10,
_ => 0,
}
}
).sum::<u8>();
println!("{r}");
}
Output as is
Basic Code
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
println!("{buf}");
}
728x90
반응형
'Development Study > Rust' 카테고리의 다른 글
[Algorithm] Baekjoon - 2차원 배열 단계 (0) | 2023.08.10 |
---|---|
[Algorithm] Baekjoon - 심화 1 단계 (0) | 2023.08.03 |
[Algorithm] Baekjoon - 1차원 배열 단계 (0) | 2023.07.13 |
[Rust] Start Rust (Day 19) - Generic Types, Traits, and Lifetimes (1) | 2023.07.01 |
[Algorithm] Baekjoon - 반복문 단계 (0) | 2023.05.24 |
Comments