Open17
AOJ - ITP1 - Rust
2_A Small, Large, or Equal
use proconio;
fn main() {
proconio::input! {
a: i8,
b: i8
}
match (a, b) {
(a,b) if a > b => println!("a > b"),
(a,b) if a < b => println!("a < b"),
(a,b) if a == b => println!("a == b"),
_ => {}
}
}
2_B Range
use proconio;
fn main() {
proconio::input! {
a: i8,
b: i8,
c: i8
}
let result = if (a < b) && (b < c) {"Yes"} else {"No"};
println!("{}", result);
}
2_C Sorting Three Numbers
use proconio;
fn main() {
proconio::input! {
a: i8,
b: i8,
c: i8
}
let mut v = vec![a,b,c];
v.sort();
println!("{} {} {}", v[0], v[1], v[2]);
}
2_D Circle in a Rectangle
structを使った解法
use proconio;
fn main() {
proconio::input! {
W: i8,
H: i8,
x: i8,
y: i8,
r: i8
}
struct Circle{
x: i8,
y: i8,
r: i8
}
impl Circle {
fn area(&self) -> Area {
// 上下左右
Area( self.y + self.r,
self.y - self.r,
self.x - self.r,
self.x + self.r )
}
}
struct Square {
W: i8,
H: i8
}
impl Square {
// 上下左右
fn area(&self) -> Area {
Area(self.H, 0, 0, self.W)
}
}
struct Area(i8, i8, i8, i8);
let c: Circle = Circle { x: x, y: y, r: r};
let s: Square = Square { W: W, H: H};
fn judge_inner(c: &Circle, s: &Square) -> bool {
let circle_area:Area = c.area();
let square_area:Area = s.area();
fn top_bottom(c_area: &Area , s_area: &Area) -> bool {
(c_area.0 < s_area.0) && (c_area.1 > s_area.1)
}
fn left_right(c_area: &Area, s_area: &Area) -> bool {
(c_area.2 > s_area.2) && (c_area.3 < s_area.3)
}
top_bottom(&circle_area, &square_area) && left_right(&circle_area, &square_area)
}
if judge_inner(&c, &s) {
println!("Yes");
} else {
println!("No");
}
}
3_A Print Many Hello World
fn main() {
for _i in 0..1000 {
println!("Hello World");
}
}
3_B Print Test Cases
use proconio;
fn main() {
let mut i = 1;
loop {
proconio::input!{
a: i8
}
if a==0 {break;}
println!("Case {}: {}", i, a);
i+=1;
}
}
3_C Swapping Two Numbers
use proconio;
use std::mem;
fn main() {
loop {
proconio::input!{
mut a: i8,
mut b: i8
}
if a == 0 && b == 0 { break }
if a > b { mem::swap(&mut a, &mut b); }
println!("{} {}", a, b);
}
}
3_D How Many Divisors?
use proconio;
fn main() {
proconio::input! {
a: i8,
b: i8,
c: i8
}
// cの約数がa~bの間で何個あるか
let mut counter = 0;
for i in a..=b {
if (c % i) == 0 {counter+=1}
}
println!("{}", counter)
}
4_A A / B Problem
ダサい。
use proconio;
fn main() {
proconio::input! {
a: i32,
b: i32,
}
print!("{} ", a / b);
print!("{} ", a%b);
println!("{}", ((a as f64)/ (b as f64) * 100000.0 ).round() / 100000.0 );
}
4_B Circle
use proconio;
use std::f64::consts::PI;
fn main() {
proconio::input! {
r: f64
}
fn error_formatter(num: f64) -> f64{
(num * 1000000.0).round() / 1000000.0
}
println!("{}", error_formatter(r * r * PI as f64));
println!("{}", error_formatter(2.0 * r * PI));
}
4_C Simple Calculator
operatorの型キャストが雑になった。
use proconio;
fn main() {
loop {
proconio::input! {
a: i32,
op: String,
b: i32
}
let op: &str = &op;
let result:i32 = match op {
"+" => a + b,
"-" => a - b,
"*" => a * b,
"/" => a / b,
_ => 0
};
if result == 0 { break }
println!("{}", result);
}
}
4_D Min, Max and Sum
use proconio;
fn main() {
proconio::input!{
n: i32,
mut array: [i32; n]
}
array.sort();
let s:i32 = array.iter().fold(0, |sum, i| sum + i);
println!("{} {} {}", array[0], array[array.len()-1], s);
}
5_A Print a Rectangle
use proconio;
fn main() {
loop {
proconio::input!{
h:usize,
w:usize
}
if h == 0 && w ==0 { break };
let yoko = "#".to_string().repeat(h);
for _i in 0..h {
println!("{}", yoko)
}
println!()
}
}
6_A Reversing Numbers
use std::io;
fn read<T: std::str::FromStr>() -> Vec<T> {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.trim().split(' ').flat_map(str::parse).collect()
}
fn main() {
let n = read::<usize>()[0];
let mut v = read::<i32>();
let mut result = "".to_string();
v.reverse();
for i in 0..n {
result = result + &v[i].to_string();
// 最後に空白を入れてはいけない。
if i < n-1 {
result = result + " ";
}
}
println!("{}", result)
}
6_B Finding Missing Cards(失敗作)
HashSet 出力がうまくいかない。
use std::io;
use std::collections::HashSet;
fn read<T: std::str::FromStr>() -> Vec<T> {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.trim().split(' ').flat_map(str::parse).collect()
}
fn main() {
let mut cards = make_all_cards();
let have_card_num = read::<i32>()[0];
for _i in 0..have_card_num {
let have_card = read::<String>();
let mut have_card = format!("{} {}", have_card[0], have_card[1]);
cards.remove(&have_card);
}
println!("{:?}",cards);
}
fn make_all_cards() -> HashSet<String> {
let mut cards: HashSet<String> = HashSet::new();
let marks:Vec<&str> = vec!["S", "H", "C","D"];
for num in 1..=13 {
for m in &marks {
let card = format!("{} {}",m, num);
cards.insert(card);
}
}
cards
}
6_C Official House ✅
一発でできた。
use std::io;
use std::cmp;
fn read<T: std::str::FromStr>() -> Vec<T> {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.trim().split(' ').flat_map(str::parse).collect()
}
#[derive(Debug, Clone)]
struct Residence {
room: Vec<Vec<i32>>
}
impl Residence {
fn new() -> Self {
Residence {room: vec![ vec![0; 10] ; 3]}
}
fn remove_minus(&mut self) {
for f in 0..self.room.len(){
for r in 0..self.room[0].len() {
self.room[f][r] = cmp::max(self.room[f][r],0)
}
}
}
fn printer(&self) {
for f in 0..self.room.len(){
for r in 0..self.room[0].len() {
print!(" {}", self.room[f][r])
}
println!("")
}
}
}
fn main() {
let mut residences: Vec<Residence> = vec![Residence::new() ; 4];
let input_num = read::<i32>()[0];
for _i in 0..input_num {
//b棟f階のr番目の部屋にv人が追加で入居したことを示します。vが負の値の場合、-v人退去したことを示します。
let info = read::<i32>();
let b = info[0] as usize -1;
let f = info[1] as usize -1;
let r = info[2] as usize -1;
let v = info[3];
residences[b].room[f][r] += v
}
for i in 0..residences.len() {
residences[i].remove_minus();
residences[i].printer();
if i < (residences.len()-1) { println!("####################")}
}
}
1_7_A Grading
条件分岐はmatchでそこそこ綺麗に処理がまとまった。
use std::io;
fn read<T: std::str::FromStr>() -> Vec<T> {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.trim().split(' ').flat_map(str::parse).collect()
}
fn main() {
loop {
let inputs = read::<i32>();
let m = inputs[0];
let f = inputs[1];
let r = inputs[2];
if m == -1 && f == -1 && r == -1 { break; }
match (m, f, r) {
(-1,f,r) => println!("F"),
(m,-1,r) => println!("F"),
(m,f,r) if m+f >= 80 => println!("A"),
(m,f,r) if 80 > m+f && m+f >= 65 => println!("B"),
(m,f,r) if 65 > m+f && m+f >= 50 => println!("C"),
(m,f,r) if 50 > m+f && m+f >= 30 && r >= 50 => println!("C"),
(m,f,r) if 50 > m+f && m+f >= 30 && r < 50 => println!("D"),
(m,f,r) if m+f < 30 => println!("F"),
_ => println!("F")
}
}
}