用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
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短