📌  相关文章
📜  打印前n个1 / n的k位数字,其中n是一个正整数

📅  最后修改于: 2021-04-24 21:58:05             🧑  作者: Mango

给定正整数n,请在点1 / n的值后打印前k个数字。您的程序应避免溢出和浮点运算。
例子 :

Input:   n = 3, k = 3
Output:  333

Input:   n = 50, k = 4
Output:  0200

强烈建议您最小化浏览器,然后自己尝试。
让我们考虑一个示例,n = 7,k =3。1/7的第一位是’1’,可以通过做10/7的整数值来获得。 10/7的余数是3。下一位是4,可以通过取30/7的整数值来获得。 30/7的余数是2。下一位数字是2,可以通过取20/7的整数值来获得

C++
#include 
using namespace std;
 
// Function to print first k digits after dot in value
// of 1/n.  n is assumed to be a positive integer.
void print(int n, int k)
{
   int rem = 1; // Initialize remainder
 
   // Run a loop k times to print k digits
   for (int i = 0; i < k; i++)
   {
         // The next digit can always be obtained as
         // doing (10*rem)/10
         cout << (10 * rem) / n;
 
         // Update remainder
         rem = (10*rem) % n;
   }
}
 
// Driver program to test above function
int main()
{
    int n = 7, k = 3;
    print(n, k);
    cout << endl;
 
    n = 21, k = 4;
    print(n, k);
 
    return 0;
}


Java
// Java code to Print first k
// digits of 1/n where n is a
// positive integer
import java.io.*;
 
class GFG
{
    // Function to print first
    // k digits after dot in value
    // of 1/n. n is assumed to be
    // a positive integer.
    static void print(int n, int k)
    {
        // Initialize remainder
        int rem = 1;
         
        // Run a loop k times to print k digits
        for (int i = 0; i < k; i++)
        {
            // The next digit can always be
            // obtained as doing (10*rem)/10
            System.out.print( (10 * rem) / n);
 
            // Update remainder
            rem = (10 * rem) % n;
             
        }
         
    }
     
    // Driver program
    public static void main(String []args)
    {
        int n = 7, k = 3;
        print(n, k);
        System.out.println();
         
        n = 21;
        k = 4;
        print(n, k);
         
    }
}
 
// This article is contributed by vt_m


Python3
# Python code to Print first k
# digits of 1/n where n is a
# positive integer
import math
 
# Function to print first k digits
# after dot in value of 1/n. n is
# assumed to be a positive integer.
def Print(n, k):
    rem = 1 # Initialize remainder
     
    # Run a loop k times to print
    # k digits
    for i in range(0, k):
        # The next digit can always
        # be obtained as doing
        # (10*rem)/10
        print(math.floor(((10 * rem)
                       / n)), end="")
         
        # Update remainder
        rem = (10*rem) % n
 
# Driver program to test
# above function
n = 7
k = 3
Print(n, k);
print(" ")
n = 21
k = 4
Print(n, k);
 
# This code is contributed by Sam007.


C#
// C# code to Print first k digits of
// 1/n where n is a positive integer
using System;
 
class GFG {
     
    // Function to print first
    // k digits after dot in value
    // of 1/n. n is assumed to be
    // a positive integer.
    static void print(int n, int k)
    {
         
        // Initialize remainder
        int rem = 1;
         
        // Run a loop k times to
        // print k digits
        for (int i = 0; i < k; i++)
        {
             
            // The next digit can always be
            // obtained as doing (10*rem)/10
            Console.Write( (10 * rem) / n);
 
            // Update remainder
            rem = (10 * rem) % n;
        }
    }
     
    // Driver program
    public static void Main()
    {
        int n = 7, k = 3;
        print(n, k);
        Console.WriteLine();
         
        n = 21;
        k = 4;
        print(n, k);
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


输出 :

142
0476