From 5e189f9bb27503068fe1f3cc29d4eb6f77698b9a Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Fri, 17 Oct 2025 13:07:18 +0800 Subject: [PATCH] Exercise2 --- Exercise/Homework2/Q1.cpp | 43 +++++++++++++++++++++++++++++++++ Exercise/Homework2/Q2.rs | 50 +++++++++++++++++++++++++++++++++++++++ Exercise/Homework2/Q3.rs | 39 ++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 Exercise/Homework2/Q1.cpp create mode 100644 Exercise/Homework2/Q2.rs create mode 100644 Exercise/Homework2/Q3.rs diff --git a/Exercise/Homework2/Q1.cpp b/Exercise/Homework2/Q1.cpp new file mode 100644 index 0000000..2adfe70 --- /dev/null +++ b/Exercise/Homework2/Q1.cpp @@ -0,0 +1,43 @@ +#include +#include +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; +} + diff --git a/Exercise/Homework2/Q2.rs b/Exercise/Homework2/Q2.rs new file mode 100644 index 0000000..72f2255 --- /dev/null +++ b/Exercise/Homework2/Q2.rs @@ -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 = 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 = 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 = 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(); +} diff --git a/Exercise/Homework2/Q3.rs b/Exercise/Homework2/Q3.rs new file mode 100644 index 0000000..84aa215 --- /dev/null +++ b/Exercise/Homework2/Q3.rs @@ -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 = 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); +} \ No newline at end of file