用C / C ++程序查找数组乘法除以n的提示?
数组乘法,我们将找到给定数组的所有元素的乘积。然后根据问题将乘积除以n。让我们举个例子-
Input: arr[] = { 12, 35, 69, 74, 165, 54};
N = 47
Output: 14说明
数组就像{12,35,69,74,165,54},所以乘法将是(12*35*69*74*165*54)=19107673200。现在,如果要除以后得到余数47它将是14。
首先将所有数字乘以%,然后乘以n,然后找到提示,但是在这种方法中,如果数字最大为2^64,则给出错误的答案。
示例
#include <stdio.h>
int main() {
int arr[] = { 12, 35, 69, 74, 165, 54};
int len = 6;
int n = 47 ;
int mul = 1;
for (int i = 0; i < len; i++)
mul = (mul * (arr[i] % n)) % n;
printf("the remainder is %d", (mul%n));
return 0;
}输出结果
the remainder is 14