summaryrefslogtreecommitdiff
path: root/0013
diff options
context:
space:
mode:
Diffstat (limited to '0013')
-rw-r--r--0013/Cargo.lock7
-rw-r--r--0013/Cargo.toml6
-rw-r--r--0013/src/main.rs33
3 files changed, 46 insertions, 0 deletions
diff --git a/0013/Cargo.lock b/0013/Cargo.lock
new file mode 100644
index 0000000..5b1a871
--- /dev/null
+++ b/0013/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "p13"
+version = "0.1.0"
diff --git a/0013/Cargo.toml b/0013/Cargo.toml
new file mode 100644
index 0000000..ce536c4
--- /dev/null
+++ b/0013/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "p13"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/0013/src/main.rs b/0013/src/main.rs
new file mode 100644
index 0000000..b5f8a62
--- /dev/null
+++ b/0013/src/main.rs
@@ -0,0 +1,33 @@
+impl Solution {
+ fn char_to_int(c: char) -> i32 {
+ match c {
+ 'I' => 1,
+ 'V' => 5,
+ 'X' => 10,
+ 'L' => 50,
+ 'C' => 100,
+ 'D' => 500,
+ 'M' => 1000,
+ _ => 0,
+ }
+ }
+ pub fn roman_to_int(s: String) -> i32 {
+ let mut sum = 0;
+ for i in 0..s.len() {
+ if i < s.len() - 1 {
+ if Self::char_to_int(s.as_bytes()[i as usize] as char)
+ < Self::char_to_int(s.as_bytes()[i+1 as usize] as char) {
+ sum = sum - Self::char_to_int(s.as_bytes()[i as usize] as char);
+ continue;
+ }
+ }
+ sum = sum + Self::char_to_int(s.as_bytes()[i as usize] as char);
+ }
+ sum
+ }
+}
+
+struct Solution {}
+fn main() {
+ println!("{}", Solution::roman_to_int("MCMXCIV".to_string()));
+} \ No newline at end of file