68 lines
1.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 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;
}
```