Signed-off-by: sairate <sairate@sina.cn>

This commit is contained in:
sairate 2025-06-14 10:54:14 +08:00
parent 1e85870eb8
commit 7a2c42089a
18 changed files with 132 additions and 50 deletions

View File

@ -10,43 +10,48 @@
``` ```
```cpp ```cpp
#include<iostream> #include<iostream> // 引入输入输出流库,用于 cin 和 cout
#include<string> #include<string> // 引入字符串库,支持 string 类型
#include<vector> #include<vector> // 引入向量容器,用于存储中间结果
using namespace std; using namespace std;
// 高精度字符串乘法res * num // 高精度字符串乘法函数:将一个大整数(字符串形式)与一个整数相乘
string multiply(string res, int num) { string multiply(string res, int num) {
vector<int> temp; vector<int> temp; // 存储乘法结果的每一位(倒序)
int carry = 0; int carry = 0; // 进位值初始化为0
// 从低位到高位逐位相乘
for (int i = res.size() - 1; i >= 0; i--) { for (int i = res.size() - 1; i >= 0; i--) {
int product = (res[i] - '0') * num + carry; int product = (res[i] - '0') * num + carry; // 当前位乘积 + 进位
temp.push_back(product % 10); temp.push_back(product % 10); // 当前位结果
carry = product / 10; carry = product / 10; // 更新进位
} }
// 处理最后的进位
while (carry) { while (carry) {
temp.push_back(carry % 10); temp.push_back(carry % 10);
carry /= 10; carry /= 10;
} }
// 把结果翻转回来 // 将结果翻转并转换为字符串
string result; string result;
for (int i = temp.size() - 1; i >= 0; i--) { for (int i = temp.size() - 1; i >= 0; i--) {
result += temp[i] + '0'; result += temp[i] + '0'; // int 转为字符
} }
return result; return result;
} }
int main() { int main() {
int n; int n;
cin >> n; cin >> n; // 输入要计算的阶乘数字 n
string result = "1"; string result = "1"; // 初始结果为 1阶乘起始值
// 从 2 乘到 n不断更新结果
for (int i = 2; i <= n; i++) { for (int i = 2; i <= n; i++) {
result = multiply(result, i); result = multiply(result, i); // 累乘
} }
cout << result << endl;
cout << result << endl; // 输出 n 的阶乘
return 0; return 0;
} }
``` ```

View File

@ -1,34 +0,0 @@
var
i,j,n,w : longint;
a : array[1..1000] of integer;
procedure fact(k : longint);
var
x,i : longint;
begin
x := 0;
for i := 1 to w do
begin
a[i] := a[i]*k+x;
x := a[i] div 10;
a[i] := a[i] mod 10;
end;
while x>0 DO
begin
w := w+1;
a[w] := x mod 10;
x := x div 10;
end;
end;
begin
assign(input,'ni.in'); reset(input);
assign(output,'ni.out'); rewrite(output);
a[1] := 1;
w := 1;
readln(n);
for i := 1 to n do fact(i);
for j := w downto 1 do write(a[j]);
writeln;
close(input); close(output);
end.

View File

@ -0,0 +1,67 @@
## 2、求A/B高精度值(ab)
### 【问题描述】
计算A/B的精确值设AB是以一般整数输入计算结果精确到小数后20位若不足20位末尾不用补0
### 【输入样例1】
```
4 3
```
### 【输出样例1】
```
4/3=1.33333333333333333333
```
### 【输入样例2】
```
6 5
```
### 【输出样例2】
```
6/5=1.2
```
### 参考代码
``` cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
long long A, B;
cin >> A >> B;
// 整数部分
long long int_part = A / B;
long long remainder = A % B;
// 输出格式
cout << A << "/" << B << "=" << int_part;
// 如果没有小数部分就直接结束
if (remainder == 0) {
cout << endl;
return 0;
}
// 有小数部分
cout << ".";
// 保留20位小数
int count = 0;
while (remainder != 0 && count < 20) {
remainder *= 10; // 模拟手算除法,小数点后向下除
cout << remainder / B; // 商为当前小数位
remainder %= B; // 更新余数
count++;
}
cout << endl;
return 0;
}
```

View File

@ -0,0 +1,30 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,m,i;
cin>>n>>m;
printf("%d/%d=%d.",n,m,n/m);
n%=m;
for (i=1;i<=20;i++)
{
n*=10;
printf("%d",n/m);
n%=m;
if (n==0) break;
}
printf("\n");
return 0;
}

View File

@ -0,0 +1 @@
4 3

View File

@ -0,0 +1 @@
4/3=1.33333333333333333333

View File

@ -0,0 +1 @@
6 5

View File

@ -0,0 +1 @@
6/5=1.2

View File

@ -0,0 +1 @@
30 6

View File

@ -0,0 +1 @@
30/6=5.0

View File

@ -0,0 +1 @@
18 5

View File

@ -0,0 +1 @@
18/5=3.6

View File

@ -0,0 +1 @@
12 7

View File

@ -0,0 +1 @@
12/7=1.71428571428571428571

View File

@ -0,0 +1 @@
8 3

View File

@ -0,0 +1 @@
8/3=2.66666666666666666666

View File

@ -0,0 +1 @@
1034 1033

View File

@ -0,0 +1 @@
1034/1033=1.000968054211035818