📜  当大数除以r时求余数的程序

📅  最后修改于: 2021-04-25 00:38:13             🧑  作者: Mango

给定数字N,当N除以R(两位数字)时,任务是找到余数。数字的输入可能非常大。
例子:

Input: N = 13589234356546756, R = 13
Output: 11

Input: N = 3435346456547566345436457867978, R = 17
Output: 13

下面是实现上述方法的程序:

C++
// CPP implementation to find Remainder
// when a large Number is divided by R
 
#include 
using namespace std;
 
// Function to Return Remainder
int Remainder(string str, int R)
{
    // len is variable to store the
    // length of Number string.
    int len = str.length();
 
    int Num, Rem = 0;
 
    // loop that find Remainder
    for (int i = 0; i < len; i++) {
 
        Num = Rem * 10 + (str[i] - '0');
        Rem = Num % R;
    }
 
    // Return the remainder
    return Rem;
}
 
// Driver code
int main()
{
 
    // Get the large number as string
    string str = "13589234356546756";
 
    // Get the divisor R
    int R = 13;
 
    // Find and print the remainder
    cout << Remainder(str, R);
 
    return 0;
}


Java
// Java implementation to find Remainder
// when a large Number is divided by R
 
class GFG
{
    // Function to Return Remainder
    static int Remainder(String str, int R)
    {
        // len is variable to store the
        // length of Number string.
        int len = str.length();
     
        int Num, Rem = 0;
     
        // loop that find Remainder
        for (int i = 0; i < len; i++) {
     
            Num = Rem * 10 + (str.charAt(i) - '0');
            Rem = Num % R;
        }
     
        // Return the remainder
        return Rem;
    }
     
    // Driver code
    public static void main( String [] args)
    {
     
        // Get the large number as string
        String str = "13589234356546756";
     
        // Get the divisor R
        int R = 13;
     
        // Find and print the remainder
        System.out.println(Remainder(str, R));
     
         
    }
}
 
// This code is contributed
// by ihritik


Python 3
# Python 3 implementation to
# find Remainder when a large
# Number is divided by R
 
# Function to Return Remainder
def Remainder(str, R):
 
    # len is variable to store the
    # length of Number string.
    l = len(str)
 
    Rem = 0
 
    # loop that find Remainder
    for i in range(l):
 
        Num = Rem * 10 + (ord(str[i]) -
                          ord('0'))
        Rem = Num % R
 
    # Return the remainder
    return Rem
 
# Driver code
if __name__ == "__main__":
 
    # Get the large number
    # as string
    str = "13589234356546756"
 
    # Get the divisor R
    R = 13
 
    # Find and print the remainder
    print(Remainder(str, R))
 
# This code is contributed
# by ChitraNayal


C#
// C# implementation to find
// Remainder when a large
// Number is divided by R
using System;
 
class GFG
{
     
// Function to Return Remainder
static int Remainder(String str,
                     int R)
{
    // len is variable to store the
    // length of Number string.
    int len = str.Length;
 
    int Num, Rem = 0;
 
    // loop that find Remainder
    for (int i = 0; i < len; i++)
    {
        Num = Rem * 10 + (str[i] - '0');
        Rem = Num % R;
    }
 
    // Return the remainder
    return Rem;
}
 
// Driver code
public static void Main()
{
 
    // Get the large number as string
    String str = "13589234356546756";
 
    // Get the divisor R
    int R = 13;
 
    // Find and print the remainder
    Console.WriteLine(Remainder(str, R));
}
}
 
// This code is contributed
// by Subhadeep


PHP


Javascript


输出:
11

时间复杂度: O(L)其中L是字符串的长度

辅助空间: O(L)