【题解】历届试题 小数第n位⭐⭐【高精度】

    xiaoxiao2022-07-05  167

    历届试题 小数第n位

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

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

    Input

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

    Output

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

    Examples

    样例输入 1 8 1 样例输出 125 样例输入 1 8 3 样例输出 500 样例输入 282866 999000 6 样例输出 914

    Hint

    题意:

    题解:

    模拟手算除法, 同时注意直接跳过处理循环位, 要不然会超时

    经验小结:

    #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <string> #include <stdlib.h> #include <vector> #include <queue> #include <cmath> #include <stack> #include <map> #include <set> using namespace std; #define ms(x, n) memset(x,n,sizeof(x)); typedef long long LL; const int inf = 1<<30; const LL maxn = 1e5+10; int main() { int a, b, sa, n; cin >> a >> b >> n; sa = a%b; for(int i = 1; i <= n; ++i){ //先处理n前的位数 sa = sa%b * 10; //下一位 if(sa%b == a%b) //处理循环位 n %= i, i = 0; } for(int i = 1; i <= 3; ++i){ printf("%d",sa/b); // sa/b表示当前位 sa = sa%b * 10; } return 0; }
    最新回复(0)