Signed-off-by: sairate <sairate@sina.cn>
This commit is contained in:
parent
1e85870eb8
commit
7a2c42089a
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -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.
|
|
||||||
67
高精算法--2.求A除B高精值/2.求A除B高精值.md
Normal file
67
高精算法--2.求A除B高精值/2.求A除B高精值.md
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
## 2、求A/B高精度值(ab)
|
||||||
|
### 【问题描述】
|
||||||
|
计算A/B的精确值,设A,B是以一般整数输入,计算结果精确到小数后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;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
30
高精算法--2.求A除B高精值/ab.cpp
Normal file
30
高精算法--2.求A除B高精值/ab.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab0.in
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab0.in
Normal file
@ -0,0 +1 @@
|
|||||||
|
4 3
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab0.out
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab0.out
Normal file
@ -0,0 +1 @@
|
|||||||
|
4/3=1.33333333333333333333
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab1.in
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab1.in
Normal file
@ -0,0 +1 @@
|
|||||||
|
6 5
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab1.out
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab1.out
Normal file
@ -0,0 +1 @@
|
|||||||
|
6/5=1.2
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab2.in
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab2.in
Normal file
@ -0,0 +1 @@
|
|||||||
|
30 6
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab2.out
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab2.out
Normal file
@ -0,0 +1 @@
|
|||||||
|
30/6=5.0
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab3.in
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab3.in
Normal file
@ -0,0 +1 @@
|
|||||||
|
18 5
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab3.out
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab3.out
Normal file
@ -0,0 +1 @@
|
|||||||
|
18/5=3.6
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab4.in
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab4.in
Normal file
@ -0,0 +1 @@
|
|||||||
|
12 7
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab4.out
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab4.out
Normal file
@ -0,0 +1 @@
|
|||||||
|
12/7=1.71428571428571428571
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab5.in
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab5.in
Normal file
@ -0,0 +1 @@
|
|||||||
|
8 3
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab5.out
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab5.out
Normal file
@ -0,0 +1 @@
|
|||||||
|
8/3=2.66666666666666666666
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab6.in
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab6.in
Normal file
@ -0,0 +1 @@
|
|||||||
|
1034 1033
|
||||||
1
高精算法--2.求A除B高精值/ab_data/ab6.out
Normal file
1
高精算法--2.求A除B高精值/ab_data/ab6.out
Normal file
@ -0,0 +1 @@
|
|||||||
|
1034/1033=1.000968054211035818
|
||||||
Loading…
x
Reference in New Issue
Block a user