LoginSignup
0
0

More than 3 years have passed since last update.

RustでAtcoderを解くときに使う処理を書いていく

Posted at

初めに

PythonでAtcoderを解いていた私が、Rustの習得のためA,B問題を取り組んだ時にググったりほかの人のコードで参考にした処理をまとめる。

制御系

if~else if~else

if a < 10 {
    // 処理a;
} else if a > 10 {
    // 処理b;
} else {
    // 処理c;
}

while

while a > 0 {
    a -= 1;
}

配列操作

空Vecの宣言

let mut ans = Vec::new();

Vecを0で初期化する

let mut rec =  vec![0; n+1];

Vecの長さを取得(sum)

println!("{}", ans.len());

Vecの合計をとる(sum)

let ans:i64 = a.iter().sum();
println!("{}", ans);

Vecの最大値・最小値をとる(i64, max,min)

iter().max()の帰り値はOptions型で、unwrapで値を取り出す

println!("{}", k-x.iter().max().unwrap());
println!("{}", k-x.iter().min().unwrap());

参考

数値計算

float(f64)型

数値を丸める(roud)

    let mut n:f64 = 100.0;
    let interest:f64 = 1.001;
    while n < x {
        n = (n*interest).round()/1.0;
    }

累乗

let b: i64 = 2;
println!("{}", b.pow(2));

文字列操作

入力

input!マクロを使う

input!{
s: String,
}

スライス

let s = String::from("hello world");

let hello = &s[0..5];
let world = &s[6..11];

参考

n文字目を取り出す


参考

型変換

ここが参考になる
https://qiita.com/smicle/items/29a4d5d1d14ad7f77f60

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0