我想找到给定数字的不同素数的数量,我将数组传递给函数,然后将数组的所有元素相乘以生成数字,我得到一些测试用例的BUS ERROR 我尝试过:
I want to find the number of distinct prime number of a given number, I am passing a array to the function and then multiplying all the elements of the array to generate the number, I am getting BUS ERROR for some test cases What I have tried:
int Solution::solve(vector<int> &A) { long long int mul = 1; for(int i=0;(i<A.size()-1);i++) { mul = mul*A[i]; } int new1 = mul; int arr[new1 + 1]; memset(arr, 0, sizeof(arr)); for (int i = 2; i * i <= new1; i++) { if (!arr[i]) { for (int j = 2 * i; j <= new1; j += i) { arr[j]++; } } arr[i] = 1; } return *max_element(arr, arr+new1); }推荐答案
这段代码很奇怪: This code is weird: for(int i=0;(i<A.size()-1);i++) { mul = mul*A[i]; }
因为它乘以所有向量但最后一个。
because it multiply all number of the vector but the last one.
Quote:我想找到给定数字的不同素数的数量
I want to find the number of distinct prime number of a given number
你可以定义这个吗? 因为我理解它,它不是你的代码,它甚至不是很接近。 据我所知,你的代码返回 mul 以下的所有素数列表。 完成代码以便运行。 显示一个输入示例,提供错误消息。
Can you define this ? Because as I understand it, it is not what is doing your code, it- is not even close. As I understand it, your code return a list of all prime numbers below mul. Complete your code so it can be run. Show an input example that give you the error message.
更多推荐
给定数字的不同素数的数量
发布评论