Exercise2

This commit is contained in:
e2hang
2025-10-17 13:07:18 +08:00
parent 163aa32d0b
commit 5e189f9bb2
3 changed files with 132 additions and 0 deletions

43
Exercise/Homework2/Q1.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include <iostream>
#include <string>
using namespace std;
int map(char c) {
if (c == '{') return 4;
if (c == '[') return 3;
if (c == '(') return 2;
if (c == '<') return 1;
return 0;
}
bool match(char a, char b) {
return (a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}') || (a == '<' && b == '>');
}
bool check(const string& s) {
string front;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '{' || s[i] == '[' || s[i] == '(' || s[i] == '<') {
if (!front.empty() && map(front.back()) <= map(s[i]))
return false;
front.push_back(s[i]);
} else if (s[i] == '}' || s[i] == ']' || s[i] == ')' || s[i] == '>') {
if (front.empty() || !match(front.back(), s[i]))
return false;
front.pop_back();
}
}
return front.empty();
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
cout << (check(s) ? "Match" : "Fail") << endl;
}
return 0;
}

50
Exercise/Homework2/Q2.rs Normal file
View File

@@ -0,0 +1,50 @@
use std::io::{self, Read, Write};
fn main() {
/*
let mut s = String::new();
io::stdin().read_line(&mut s).unwrap();
let n: usize = s.trim().parse().unwrap();
let mut nums: Vec<i64> = Vec::new();
let mut max: i64 = 0;
for _ in 0..n {
let mut x = String::new();
io::stdin().read_line(&mut x).unwrap();
let a: i64 = x.trim().parse().unwrap();
nums.push(a);
if a > max {
max = a;
}*/
// //输入太慢了过不了
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let mut it = buf.split_whitespace();
let m: usize = it.next().unwrap().parse().unwrap();
let mut vec: Vec<i64> = Vec::with_capacity(m);
let mut max: i64 = 0;
for _ in 0..m {
let a: i64 = it.next().unwrap().parse().unwrap();
vec.push(a);
if a > max { max = a; }
}
let mut dp: Vec<i64> = vec![0; (max + 1) as usize];
if max >= 0 { dp[0] = 1; }
if max >= 1 { dp[1] = 1; }
if max >= 2 { dp[2] = 2; }
if max >= 3 { dp[3] = 4; }
for i in 4..=max {
dp[i as usize] = dp[(i - 1) as usize] + dp[(i - 2) as usize] + dp[(i - 3) as usize] + dp[(i - 4) as usize];
}
let mut out = String::new();
for i in vec {
out.push_str(&format!("{}\n", dp[i as usize]));
}
io::stdout().write_all(out.as_bytes()).unwrap();
}

39
Exercise/Homework2/Q3.rs Normal file
View File

@@ -0,0 +1,39 @@
use std::io;
pub struct Status{
pos: i32,
a: char, // from
b: char, // to
c: char // aux
}
fn main(){
let mut s = String::new();
io::stdin().read_line(&mut s).unwrap();
let n: i32 = s.trim().parse().unwrap();
let mut stack: Vec<Status> = Vec::new();
stack.push(Status{pos: n, a: 'A', b: 'C', c: 'B'});
let mut max = 0;
let mut output = String::new(); //输出,要不然太慢总会超时
while !stack.is_empty() {
if stack.len() > max {
max = stack.len();
}
let mut end = stack.pop().unwrap();
if end.pos == 1 {
output.push_str(&format!("Move disk from {} to {}\n", end.a, end.b));
} else {
stack.push(Status{pos: end.pos - 1, a: end.c, b: end.b, c: end.a});
stack.push(Status{pos: 1, a: end.a, b: end.b, c: end.c});
stack.push(Status{pos: end.pos - 1, a: end.a, b: end.c, c: end.b});
}
}
print!("{}", output);
println!("{}", max);
}