• 小数第n位


    问题描述
      我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数。
      如果我们把有限小数的末尾加上无限多个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
     
    一方面暴力法,可以先每次再末尾加10个0,后面再一个一个加0。
    另一方面题目说肯定是有限或者无限循环的,所以根据a%b是否和最初的a%b相同可以得到循环节从而简化中间的步骤。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <map>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    typedef long long ll;
    ll d = 1e10;
    ll a;
    int b,n;
    int c;
    int main() {
        scanf("%lld%d%d",&a,&b,&n);
        while(c < n) {
            a %= b;
            if(c + 10 < n) {
                a *= d;
                c += 10;
            }
            else {
                a *= 10;
                c ++;
            }
        }
        for(int i = 0;i < 3;i ++) {
            printf("%d",a / b);
            a %= b;
            a *= 10;
        }
    }
    #include <iostream>
    #include <cstdio>
    #include <map>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    typedef long long ll;
    ll a;
    int b,n;
    int c;
    int s = -1;
    int main() {
        scanf("%lld%d%d",&a,&b,&n);
        while(c < n) {
            a %= b;
            if(a == 0) break;
            if(a == s) {
                n %= c;
                c = 0;
            }
            else if(s == -1) s = a;
            a *= 10;
            c ++;
        }
        for(int i = 0;i < 3;i ++) {
            printf("%d",a / b);
            a %= b;
            a *= 10;
        }
    }
  • 相关阅读:
    [leetcode]Combination SumII
    NSum小结
    image 与 canvas 的相互转化
    $_SERVER 相关变量
    最近做的项目,总结一下。
    开始在博客园,写博客,先写点东西。
    Codeforces Round #584 C. Paint the Digits
    Codeforces Round #584 B. Koala and Lights
    Codeforces Round #584 A. Paint the Numbers
    HDU-2296-Ring(AC自动机, DP)
  • 原文地址:https://www.cnblogs.com/8023spz/p/10390779.html
Copyright © 2020-2023  润新知