给定一个大数字作为字符串,找到除以7的数字的余数。
例子 :
Input : num = 1234
Output : 2
Input : num = 1232
Output : 0
Input : num = 12345
Output : 4
简单的方法是将字符串转换为数字并执行mod操作。但是这种方法不适用于长字符串。
有一种方法也适用于大量人员。以下是步骤。
让给定的数字为“ num”
- 我们使用1、3、2,-1,-3,-2的序列来查找余数(下面讨论该序列的直觉)。
- 将结果初始化为0。
- 从结尾遍历num,从开头开始遍历序列。对于每个遍历的数字,将其与序列的下一个数字相乘,然后相乘得出结果。
- 在有更多数字要处理时,请继续重复第3步。如果位数超过6(序列中的术语数),则从头开始遍历序列。
- 在每一步之后,我们继续执行result = result%7,以确保结果保持小于7。
插图 :
let us take above Example where number is 12345.
We reverse the number from end and series from
the beginning and keep adding multiplication to
the result.
(12345 % 7) = (5*1 + 4*3 + 3*2 + 2*(-1) + 1*(-3)) % 7
= (5 + 12 + 6 - 2 - 3)%7
= (18) % 7
= 4
hence 4 will be the remainder when we divide
the number 12345 by 7.
此系列公式如何工作?
以下是该系列背后的直觉
1 % 7 = 1
10 % 7 = 3
100 % 7 = 2
1000 % 7 = 6 = (-1) % 7
10000 % 7 = 4 = (-3) % 7
100000 % 7 = 5 = (-2) % 7
The series repeats itself for larger powers
1000000 % 7 = 1
10000000 % 7 = 3
..............
...............
The above property of modular division with 7 and
associative properties of modular arithmetic are
the basis of the approach used here.
执行:
C++
// C++ program to find remainder of a large
// number when divided by 7.
#include
using namespace std;
/* Function which returns Remainder after dividing
the number by 7 */
int remainderWith7(string num)
{
// This series is used to find remainder with 7
int series[] = {1, 3, 2, -1, -3, -2};
// Index of next element in series
int series_index = 0;
int result = 0; // Initialize result
// Traverse num from end
for (int i=num.size()-1; i>=0; i--)
{
/* Find current digit of nun */
int digit = num[i] - '0';
// Add next term to result
result += digit * series[series_index];
// Move to next term in series
series_index = (series_index + 1) % 6;
// Make sure that result never goes beyond 7.
result %= 7;
}
// Make sure that remainder is positive
if (result < 0)
result = (result + 7) % 7;
return result;
}
/* Driver program */
int main()
{
string str = "12345";
cout << "Remainder with 7 is "
<< remainderWith7(str);
return 0;
}
Java
// Java program to find remainder of a large
// number when divided by 7.
class GFG
{
// Function which return Remainder
// after dividingthe number by 7
static int remainderWith7(String num)
{
// This series is used to
// find remainder with 7
int series[] = {1, 3, 2, -1, -3, -2};
// Index of next element in series
int series_index = 0;
// Initialize result
int result = 0;
// Traverse num from end
for (int i = num.length() - 1; i >= 0; i--)
{
/* Find current digit of nun */
int digit = num.charAt(i) - '0';
// Add next term to result
result += digit * series[series_index];
// Move to next term in series
series_index = (series_index + 1) % 6;
// Make sure that result never goes beyond 7.
result %= 7;
}
// Make sure that remainder is positive
if (result < 0)
result = (result + 7) % 7;
return result;
}
// Driver code
public static void main (String[] args)
{
String str = "12345";
System.out.print("Remainder with 7 is "
+remainderWith7(str));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to find remainder of
# a large number when divided by 7.
# Function which return Remainder
# after dividing the number by 7
def remainderWith7(num):
# This series is used to
# find remainder with 7
series = [1, 3, 2, -1, -3, -2];
# Index of next element
# in series
series_index = 0;
# Initialize result
result = 0;
# Traverse num from end
for i in range((len(num) - 1), -1, -1):
# Find current digit of num
digit = ord(num[i]) - 48;
# Add next term to result
result += digit * series[series_index];
# Move to next term in series
series_index = (series_index + 1) % 6;
# Make sure that result
# never goes beyond 7.
result %= 7;
# Make sure that remainder
# is positive
if (result < 0):
result = (result + 7) % 7;
return result;
# Driver Code
str = "12345";
print("Remainder with 7 is",
remainderWith7(str));
# This code is contributed by mits
C#
// C# program to find the remainder of
// a large number when divided by 7.
using System;
class GFG
{
// Function which return Remainder
// after dividingthe number by 7
static int remainderWith7(String num)
{
// This series is used to
// find remainder with 7
int []series = {1, 3, 2, -1, -3, -2};
// Index of next element in series
int series_index = 0;
// Initialize result
int result = 0;
// Traverse num from end
for (int i = num.Length - 1; i >= 0; i--)
{
/* Find current digit of nun */
int digit = num[i] - '0';
// Add next term to result
result += digit * series[series_index];
// Move to next term in series
series_index = (series_index + 1) % 6;
// Make sure that result never goes beyond 7.
result %= 7;
}
// Make sure that remainder is positive
if (result < 0)
result = (result + 7) % 7;
return result;
}
// Driver code
public static void Main ()
{
String str = "12345";
Console.Write("Remainder with 7 is " +
remainderWith7(str));
}
}
// This code is contributed by nitin mittal.
PHP
= 0; $i--)
{
// Find current digit of num
$digit = $num[$i] - '0';
// Add next term to result
$result += $digit *
$series[$series_index];
// Move to next term in series
$series_index = ($series_index + 1) % 6;
// Make sure that result
// never goes beyond 7.
$result %= 7;
}
// Make sure that remainder
// is positive
if ($result < 0)
$result = ($result + 7) % 7;
return $result;
}
// Driver Code
{
$str = "12345";
echo "Remainder with 7 is ",
(remainderWith7($str));
return 0;
}
// This code is contributed by nitin mittal.
?>
Javascript
输出:
Remainder with 7 is 4