diff --git a/高精算法---1.求n!的值/1.求n!的值.md b/高精算法---1.求n!的值/1.求n!的值.md index ae4aada..35f1e34 100644 --- a/高精算法---1.求n!的值/1.求n!的值.md +++ b/高精算法---1.求n!的值/1.求n!的值.md @@ -10,43 +10,48 @@ ``` ```cpp -#include -#include -#include +#include // 引入输入输出流库,用于 cin 和 cout +#include // 引入字符串库,支持 string 类型 +#include // 引入向量容器,用于存储中间结果 using namespace std; -// 高精度字符串乘法:res * num +// 高精度字符串乘法函数:将一个大整数(字符串形式)与一个整数相乘 string multiply(string res, int num) { - vector temp; - int carry = 0; + vector temp; // 存储乘法结果的每一位(倒序) + int carry = 0; // 进位值初始化为0 + + // 从低位到高位逐位相乘 for (int i = res.size() - 1; i >= 0; i--) { - int product = (res[i] - '0') * num + carry; - temp.push_back(product % 10); - carry = product / 10; + int product = (res[i] - '0') * num + carry; // 当前位乘积 + 进位 + temp.push_back(product % 10); // 当前位结果 + carry = product / 10; // 更新进位 } + + // 处理最后的进位 while (carry) { temp.push_back(carry % 10); carry /= 10; } - // 把结果翻转回来 + // 将结果翻转并转换为字符串 string result; for (int i = temp.size() - 1; i >= 0; i--) { - result += temp[i] + '0'; + result += temp[i] + '0'; // int 转为字符 } return result; } int main() { int n; - cin >> n; - string result = "1"; + cin >> n; // 输入要计算的阶乘数字 n + string result = "1"; // 初始结果为 1(阶乘起始值) + // 从 2 乘到 n,不断更新结果 for (int i = 2; i <= n; i++) { - result = multiply(result, i); + result = multiply(result, i); // 累乘 } - cout << result << endl; + + cout << result << endl; // 输出 n 的阶乘 return 0; } - ``` \ No newline at end of file diff --git a/高精算法---1.求n!的值/ni.pas b/高精算法---1.求n!的值/ni.pas deleted file mode 100644 index a4c9107..0000000 --- a/高精算法---1.求n!的值/ni.pas +++ /dev/null @@ -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. diff --git a/高精算法--2.求A除B高精值/2.求A除B高精值.md b/高精算法--2.求A除B高精值/2.求A除B高精值.md new file mode 100644 index 0000000..36ce0b7 --- /dev/null +++ b/高精算法--2.求A除B高精值/2.求A除B高精值.md @@ -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 +#include +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; +} + +``` + diff --git a/高精算法--2.求A除B高精值/ab.cpp b/高精算法--2.求A除B高精值/ab.cpp new file mode 100644 index 0000000..d19f157 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +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; + } + + + diff --git a/高精算法--2.求A除B高精值/ab_data/ab0.in b/高精算法--2.求A除B高精值/ab_data/ab0.in new file mode 100644 index 0000000..109c32b --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab0.in @@ -0,0 +1 @@ +4 3 diff --git a/高精算法--2.求A除B高精值/ab_data/ab0.out b/高精算法--2.求A除B高精值/ab_data/ab0.out new file mode 100644 index 0000000..d74a651 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab0.out @@ -0,0 +1 @@ +4/3=1.33333333333333333333 diff --git a/高精算法--2.求A除B高精值/ab_data/ab1.in b/高精算法--2.求A除B高精值/ab_data/ab1.in new file mode 100644 index 0000000..6ee96c1 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab1.in @@ -0,0 +1 @@ +6 5 \ No newline at end of file diff --git a/高精算法--2.求A除B高精值/ab_data/ab1.out b/高精算法--2.求A除B高精值/ab_data/ab1.out new file mode 100644 index 0000000..dfaf698 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab1.out @@ -0,0 +1 @@ +6/5=1.2 diff --git a/高精算法--2.求A除B高精值/ab_data/ab2.in b/高精算法--2.求A除B高精值/ab_data/ab2.in new file mode 100644 index 0000000..f37d2f8 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab2.in @@ -0,0 +1 @@ +30 6 \ No newline at end of file diff --git a/高精算法--2.求A除B高精值/ab_data/ab2.out b/高精算法--2.求A除B高精值/ab_data/ab2.out new file mode 100644 index 0000000..6d113d4 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab2.out @@ -0,0 +1 @@ +30/6=5.0 diff --git a/高精算法--2.求A除B高精值/ab_data/ab3.in b/高精算法--2.求A除B高精值/ab_data/ab3.in new file mode 100644 index 0000000..e1bb4a2 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab3.in @@ -0,0 +1 @@ +18 5 \ No newline at end of file diff --git a/高精算法--2.求A除B高精值/ab_data/ab3.out b/高精算法--2.求A除B高精值/ab_data/ab3.out new file mode 100644 index 0000000..f550a37 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab3.out @@ -0,0 +1 @@ +18/5=3.6 diff --git a/高精算法--2.求A除B高精值/ab_data/ab4.in b/高精算法--2.求A除B高精值/ab_data/ab4.in new file mode 100644 index 0000000..2637872 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab4.in @@ -0,0 +1 @@ +12 7 diff --git a/高精算法--2.求A除B高精值/ab_data/ab4.out b/高精算法--2.求A除B高精值/ab_data/ab4.out new file mode 100644 index 0000000..dbb739e --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab4.out @@ -0,0 +1 @@ +12/7=1.71428571428571428571 diff --git a/高精算法--2.求A除B高精值/ab_data/ab5.in b/高精算法--2.求A除B高精值/ab_data/ab5.in new file mode 100644 index 0000000..110498e --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab5.in @@ -0,0 +1 @@ +8 3 diff --git a/高精算法--2.求A除B高精值/ab_data/ab5.out b/高精算法--2.求A除B高精值/ab_data/ab5.out new file mode 100644 index 0000000..bfc9b8e --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab5.out @@ -0,0 +1 @@ +8/3=2.66666666666666666666 diff --git a/高精算法--2.求A除B高精值/ab_data/ab6.in b/高精算法--2.求A除B高精值/ab_data/ab6.in new file mode 100644 index 0000000..b3e0c03 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab6.in @@ -0,0 +1 @@ +1034 1033 \ No newline at end of file diff --git a/高精算法--2.求A除B高精值/ab_data/ab6.out b/高精算法--2.求A除B高精值/ab_data/ab6.out new file mode 100644 index 0000000..84112b7 --- /dev/null +++ b/高精算法--2.求A除B高精值/ab_data/ab6.out @@ -0,0 +1 @@ +1034/1033=1.000968054211035818