From 7dcf8f5b7b64c157e711e3b94b3c4c486c1b6c79 Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Wed, 31 Jan 2024 12:09:17 +0800 Subject: [PATCH] chore: book/ch1 add std --- .../problem/book/ch1/1. factorial/config.json | 15 ++++++ .../problem/book/ch1/1. factorial/std/ans.cpp | 46 +++++++++++++++++++ .../problem/book/ch1/1. factorial/std/ans.py | 5 ++ .../problem/book/ch1/1. factorial/std/ans.rs | 36 +++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 resource/runner/problem/book/ch1/1. factorial/std/ans.cpp create mode 100644 resource/runner/problem/book/ch1/1. factorial/std/ans.py create mode 100644 resource/runner/problem/book/ch1/1. factorial/std/ans.rs diff --git a/resource/runner/problem/book/ch1/1. factorial/config.json b/resource/runner/problem/book/ch1/1. factorial/config.json index 4abc448..462cd61 100644 --- a/resource/runner/problem/book/ch1/1. factorial/config.json +++ b/resource/runner/problem/book/ch1/1. factorial/config.json @@ -9,6 +9,21 @@ "Lang" : "cpp", "Judge": {"Type": "default", "Script": "", "Cmp": "HCMP"}, "Runtime": {"Run": {"TimeLimit": 1000, "MemoryLimit": 16, "NProcLimit": 1}} + }, + { + "Lang" : "python3", + "Judge" : {"Type": "default", "Script": "", "Cmp": "HCMP"}, + "Runtime": {"Run": {"TimeLimit": 2000, "MemoryLimit": 16, "NProcLimit": 1}} + }, + { + "Lang" : "pypy3", + "Judge" : {"Type": "default", "Script": "", "Cmp": "HCMP"}, + "Runtime": {"Run": {"TimeLimit": 2000, "MemoryLimit": 128, "NProcLimit": 1}} + }, + { + "Lang" : "rust", + "Judge" : {"Type": "default", "Script": "", "Cmp": "HCMP"}, + "Runtime": {"Run": {"TimeLimit": 1000, "MemoryLimit": 16, "NProcLimit": 1}} } ], "Tasks" : [ diff --git a/resource/runner/problem/book/ch1/1. factorial/std/ans.cpp b/resource/runner/problem/book/ch1/1. factorial/std/ans.cpp new file mode 100644 index 0000000..2ee9bc4 --- /dev/null +++ b/resource/runner/problem/book/ch1/1. factorial/std/ans.cpp @@ -0,0 +1,46 @@ +#include + +#define LEN 1000 + +int a[LEN], c[LEN]; + +void clear(int a[]) { + for (int i = 0; i < LEN; ++i) a[i] = 0; +} + +void print(int a[]) { + int i; + for (i = LEN - 1; i >= 1; --i) + if (a[i] != 0) break; + for (; i >= 0; --i) putchar(a[i] + '0'); + putchar('\n'); +} + +void mul(int a[], int b, int c[]) { + clear(c); + for (int i = 0; i < LEN - 1; ++i) { + c[i] += a[i] * b; + if (c[i] >= 10) { + c[i + 1] += c[i] / 10; + c[i] %= 10; + } + } +} + +int main() { + int n; + clear(a), clear(c); + scanf("%d", &n); + + a[0] = 1; + int *p1 = a, *p2 = c; + + for (int i = 2; i <= n; ++i) { + mul(p1, i, p2); + std::swap(p1, p2); + } + + print(p1); + + return 0; +} \ No newline at end of file diff --git a/resource/runner/problem/book/ch1/1. factorial/std/ans.py b/resource/runner/problem/book/ch1/1. factorial/std/ans.py new file mode 100644 index 0000000..491dbde --- /dev/null +++ b/resource/runner/problem/book/ch1/1. factorial/std/ans.py @@ -0,0 +1,5 @@ +n = int(input().strip()) +a = 1 +for i in range(1, n + 1): + a *= i +print(a) diff --git a/resource/runner/problem/book/ch1/1. factorial/std/ans.rs b/resource/runner/problem/book/ch1/1. factorial/std/ans.rs new file mode 100644 index 0000000..b7c0829 --- /dev/null +++ b/resource/runner/problem/book/ch1/1. factorial/std/ans.rs @@ -0,0 +1,36 @@ +use std::io; + +// Powered by Github Copilot :) + +fn factorial(n: u64) -> Vec { + let mut result = vec![1]; + for i in 2..=n { + let mut carry = 0; + for num in &mut result { + *num = *num * i + carry; + carry = *num / 10; + *num %= 10; + } + while carry > 0 { + result.push(carry % 10); + carry /= 10; + } + } + result +} + +fn print_factorial(result: Vec) { + for num in result.iter().rev() { + print!("{}", num); + } + println!(); +} + +fn main() { + let mut input = String::new(); + io::stdin().read_line(&mut input).unwrap(); + let n: u64 = input.trim().parse().unwrap(); + + let result = factorial(n); + print_factorial(result); +}