일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Operating System
- 자바
- Reversing
- Rust
- 러스트 프로그래밍 공식 가이드
- C
- 백준
- 알고리즘
- Python
- 백준 러스트
- Python challenge
- 파이썬 챌린지
- 오라클
- 데이터베이스
- 파이썬 알고리즘
- Database
- 러스트 예제
- 러스트
- 운영체제
- java
- data communication
- 자바 기초
- ubuntu
- OS
- 파이썬
- 우분투
Archives
- Today
- Total
IT’s Portfolio
[Algorithm] Baekjoon - 반복문 단계 본문
728x90
반응형
💻 Baekjoon Loop Stage
Multiplication Table
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 n: i32 = input().parse().unwrap();
for i in 1..10 {
println!("{n} * {i} = {}", n*i);
}
}
A+B - 3
Basic Code
use std::io;
fn result() -> i32 {
let mut buf = String::new();
let mut r = 0;
io::stdin().read_line(&mut buf).unwrap();
buf.pop();
buf.split(' ')
.for_each(|x| r+=x.parse::<i32>().unwrap());
r
}
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.pop();
let t = buf.parse::<i32>().unwrap();
let mut v: Vec<i32> = Vec::new();
for _ in 0..t {
v.push(result());
}
v.iter()
.for_each(|x| println!("{x}"));
}
Sum
Basic Code - for_each
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf)
.unwrap();
buf.pop();
let n = buf.parse::<i32>().unwrap();
let mut s = 0;
(1..=n).for_each(
|x| {
s+=x;
}
);
println!("{s}");
}
Basic Code - formula
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf)
.unwrap();
buf.pop();
let n = buf.parse::<i32>().unwrap();
println!("{}", n*(n+1)/2)
}
Receipt
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 x = input().parse::<i32>().unwrap();
let n = input().parse::<i32>().unwrap();
let mut s = 0;
(0..n).for_each(
|_| {
let mut tmp = 1;
input().split(' ').for_each(
|y| {
tmp*=y.parse::<i32>().unwrap()
}
);
s+=tmp;
}
);
println!("{}", if x==s { "Yes" } else { "No" });
}
Coding is Physical Education
Basic Code - 1
use std::io;
fn main() {
let mut s = String::new();
let l = "long ";
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.pop();
let n = {
(buf.parse::<i32>().unwrap())/4
};
(0..n).for_each(
|_| s.push_str(l)
);
println!("{}int", s);
}
Basic Code - 2
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.pop();
let n = buf.parse::<i32>().unwrap();
for _ in 0..n>>2 {
print!("long ");
}
print!("int");
}
Quick A+B
Basic Code
use std::io::{self, Write};
fn main() {
let mut out = io::BufWriter::new(io::stdout());
for l in io::stdin().lines().skip(1) {
let r = l.unwrap().split(' ').map(
|x| x.parse::<i32>().unwrap()
).sum::<i32>();
writeln!(out, "{}", r).unwrap();
}
}
lines()
& skip()
// data.txt
5
1 1
12 34
5 500
40 60
1000 1000
use std::io::Read;
fn main() {
let mut file = std::fs::File::open("src/data.txt").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
for l in contents.lines().skip(1) {
print!("{l}: ");
let r = l.split(' ').map(
|x| x.parse::<i32>().unwrap()
).sum::<i32>();
println!("{r}");
}
}
// Result
// 1 1: 2
// 12 34: 46
// 5 500: 505
// 40 60: 100
// 1000 1000: 2000
A+B - 7
Basic Code
use std::io::{self, Write};
fn main() {
let mut cnt: i32 = 1;
let mut out = io::BufWriter::new(io::stdout());
for l in io::stdin().lines().skip(1) {
let r = l.unwrap().split(' ').map(
|x| x.parse::<i32>().unwrap()
).sum::<i32>();
writeln!(out, "Case #{cnt}: {r}").unwrap();
cnt+=1;
}
}
A+B - 8
Basic Code
use std::io::{self, Write};
fn main() {
let mut cnt = 1;
let mut out = io::BufWriter::new(io::stdout());
for l in io::stdin().lines().skip(1) {
let r: Vec<i32> = l.unwrap().split(' ').map(
|x| x.parse::<i32>().unwrap()
).collect();
writeln!(
out, "Case #{cnt}: {} + {} = {}",
r[0], r[1], r.iter().sum::<i32>()
).unwrap();
cnt+=1;
}
}
Draw Stars - 1
Basic Code - for & for_each
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.pop();
for i in 1..=buf.parse::<i32>().unwrap() {
(0..i).for_each(|_| print!("*"));
print!("\n");
}
}
Basic Code - only for_each
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.pop();
(1..=buf.parse::<usize>().unwrap()).for_each(
|x| println!("{}", "*".repeat(x))
);
}
Draw Stars - 2
Basic Code
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.pop();
let n = buf.parse::<usize>().unwrap();
(1..=n).for_each(
|x| println!(
"{}{}", " ".repeat(n-x), "*".repeat(x)
)
);
}
A+B - 5
Basic Code
use std::io::{self, Write};
fn main() {
let mut out = io::BufWriter::new(io::stdout());
for l in io::stdin().lines() {
let r = l.unwrap().trim().split(' ').map(
|x| x.parse::<i32>().unwrap()
).sum::<i32>();
if r != 0 {
writeln!(out, "{r}").unwrap();
}
}
}
A+B - 4
Basic Code
use std::io::{self, Write};
fn main() {
let mut out = io::BufWriter::new(io::stdout());
for l in io::stdin().lines() {
let r = l.unwrap().trim().split(' ').map(
|x| x.parse::<i32>().unwrap()
).sum::<i32>();
writeln!(out, "{r}").unwrap();
}
}
728x90
반응형
'Development Study > Rust' 카테고리의 다른 글
[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.04.04 |
[Rust] Start Rust (Day 18) - Error Handling (0) | 2023.03.09 |
[Rust] Start Rust (Day 17) - Rust Example Script 5 [Simple Employees DataBase] (3) | 2023.02.27 |
Comments