蓝桥杯 PREV-52 小数第n位

试题 历届试题 小数第n位

资源限制
时间限制:1.0s 内存限制:256.0MB

问题描述

我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数。
  如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式。

本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始的3位数。

输入格式

一行三个整数:a b n,用空格分开。a是被除数,b是除数,n是所求的小数后位置(0<a,b,n<1000000000)

输出格式

一行3位数字,表示:a除以b,小数后第n位开始的3位数字。

样例输入

1 8 1

样例输出

125

样例输入

1 8 3

样例输出

500

样例输入

282866 999000 6

样例输出

914

思路:

我们要找出第几位后的后三位,因为我们是除上 b b b的,所以余下小数点后的数字可以看成就是 % b \%b %b的值,然后我们想要求第 n n n位的话,我们就要知道前面的 n − 1 n-1 n1 % b \%b %b的值,然后 ∗ 10 / b *10/b 10/b就是第 n n n位的答案了,然后运行三次就是最终的答案。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
ll quickpow(ll a, ll b, ll c) {
    ll ans = 1, base = a;
    while (b) {
        if (b & 1) ans = ans * base % c;
        base = base * base % c;
        b >>= 1;
    }
    return ans;
}
int main() {
    ll a, b, n;
    read(a), read(b), read(n);
    ll x = a * quickpow(10, n - 1, b) % b;
    for (int i = 0; i < 3; i++) {
        out(x * 10 / b);
        x = x * 10 % b;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值