链接:https://ac.nowcoder.com/acm/contest/318/D
来源:牛客网
题目描述
远神最近对高精度感兴趣,给小萌新出了道简单的除法题,a除b保留n位小数。但是越老和翔神觉得太简单了,就多加了两种操作。
翔化,结果向下取整。
越化,结果最后一位四舍五入。
输入描述:
多组输入。
第一行输入三个数a,b,n(1≤a≤100000,1≤b≤100000,0≤n≤1000)。
第二行一个字符串Xiang,Yue表示翔化和越化。
输出描述:
输出翔化或者越化的结果。
示例1
输入
复制
2 3 5
Xiang
2 3 5
Yue
输出
复制
0.66666
0.66667
最开始的代码,不知道哪错了
#include <stdio.h>
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
ll a, b, n;
while (~scanf("%lld %lld %lld", &a, &b, &n)) {
string str;
cin >> str;
ll z = a / b;
while (a >= b) a -= b;
if (str[0] == 'Y') {
ll s[1005] = {0};
for (int i = 1; i <= n+1; i++) {
a *= 10;
s[i] = a / b;
a = a % b;
}
if (s[n+1] <= 4) {
printf("%lld", z);
if (n != 0) {
printf(".");
for (ll i = 1; i <= n; i++) printf("%lld", s[i]);
}
} else {
for (ll i = n; i >= 1; i--) {
s[i]++;
if (s[i] >= 10) {
s[i-1]++;
s[i] -= 10;
} else break;
}
if (n == 0) {
if (s[1] >= 5) printf("%lld", z+1);
else printf("%lld", z);
} else printf("%lld", z+s[0]);
if (n != 0) {
printf(".");
for (ll i = 1; i <= n; i++) printf("%lld", s[i]);
}
}
printf("\n");
} else {
ll s[1005] = {0};
for (ll i = 1; i <= n; i++) {
a *= 10;
s[i] = a / b;
a %= b;
}
printf("%lld", z);
if (n != 0) {
printf(".");
for (ll i = 1; i <= n; i++) printf("%lld", s[i]);
}
printf("\n");
}
}
return 0;
}
正确代码待实现