AC043 in rust, finished all. (D doesn't get tested correctly locally)

This commit is contained in:
2022-05-28 14:58:05 +02:00
parent b378c4cb07
commit 97ee8adbf3
6 changed files with 141 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
use proconio::input;
fn main() {
input!{
n: u32,
};
let result = n * (n + 1) / 2;
println!("{}", {result});
}

View File

@@ -0,0 +1,21 @@
use proconio::input;
fn main() {
input!{
s: String,
};
let mut result = String::new();
for c in s.chars() {
if c == '0' {
result.push('0');
} else if c == '1' {
result.push('1');
} else {
result.pop();
}
}
println!("{}", {result});
}

View File

@@ -0,0 +1,28 @@
use proconio::input;
fn cost(target: i32, numbers: &[i32]) -> i32 {
let mut result = 0;
for n in numbers {
result += (target - n) * (target - n)
}
result
}
fn main() {
input!{
n: i32,
a: [i32; n],
};
let mut min = std::i32::MAX;
for t in -100..=100 {
let r = cost(t, &a);
if r < min {
min = r;
}
}
println!("{}", {min});
}

View File

@@ -0,0 +1,44 @@
use proconio::input;
fn main() {
input!{
s: String,
};
let mut cs = s.chars();
let l = s.len();
if l == 2 {
let a = cs.next().unwrap();
let b = cs.next().unwrap();
if a == b {
println!("1 2");
} else{
println!("-1 -1");
}
return
}
let mut past_chars = [' '; 3];
past_chars[1] = cs.next().unwrap();
past_chars[2] = cs.next().unwrap();
for i in 2..l {
past_chars[0] = past_chars[1];
past_chars[1] = past_chars[2];
past_chars[2] = cs.next().unwrap();
let mut sorting = [' '; 3];
sorting.copy_from_slice(&past_chars);
sorting.sort();
if sorting[0] == sorting[1] || sorting[1] == sorting[2] {
println!("{} {}", {i-1}, {i+1});
return;
}
}
println!("-1 -1");
}