使用cpp查找数组中素数个数的程序
program to find The number of prime numbers in array using cpp
这就是问题所在
enter link description here
函数 prime 中的问题,但它认为是真的,我找不到解决方案
我在 codeforces 中提交了它,但它给了我。测试 5 的错误答案
:-
输入:
39
81 46 4 5 2 71 66 97 51 84 50 64 68 99 58 45 64 86 14 44 7 49 45 72 94 19 33 68 83 12 89 88 39 36 51 11 57 9 54
the wrong !!
#include <iostream>
#include <math.h>
using namespace std;
int maximum(int arr[], int n)
{
int max = INT_MIN;
for (int i = 0; i < n; i++)
{
if (max < arr[i]) { max = arr[i]; }
}
return max;
}
int minimum(int arr[], int n)
{
int min = INT_MAX;
for (int i = 0; i < n; i++)
{
if (min > arr[i]) { min = arr[i]; }
}
return min;
}
int prime(int arr[], int n)
{
int con = 0;
bool flag = true;
for (int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
con++;
}
else if (arr[i] > 2)
{
for (int j = 2; j < n; j++)
{
if (arr[i] % j == 0)
{
flag = false;
break;
}
else
{
flag = true;
}
}
if (flag == true)
con++;
}
}
return con;
}
int palindrome(int arr[], int n)
{
int i = 0, con = 0;
while (n--)
{
int temp;
temp = arr[i];
int reverseNumber = 0, rightDigit;
while (temp != 0)
{
rightDigit = temp % 10;
reverseNumber = (reverseNumber * 10) + rightDigit;
temp = temp / 10;
}
if (reverseNumber == arr[i]) {
con++;
}
i++;
}
return con;
}
int divisors(int arr[], int n)
{
int max = 0;
int con = 0;
int x = arr[0];
for (int i = 0; i < n; i++)
{
int temp = arr[i];
for (int j = 1; j <= arr[i]; j++)
{
if (arr[i] % j == 0)
{
con++;
}
}
if (max < con)
{
max = con;
x = arr[i];
}
else if (max == con)
{
if (x < arr[i])
{
x = arr[i];
}
}
con = 0;
}
return x;
}
int main()
{
int n; cin >> n;
int arr[1001];
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "The maximum number : " << maximum(arr, n) << endl;
cout << "The minimum number : " << minimum(arr, n) << endl;
cout << "The number of prime numbers : " << prime(arr, n) << endl;
cout << "The number of palindrome numbers : " << palindrome(arr, n) << endl;
cout << "The number that has the maximum number of divisors : " << divisors(arr, n) << endl;
divisors(arr, n);
return 0;
}
这个for循环
for (int j = 2; j < n; j++)
不正确。看来你的意思是
for (int j = 2; j < arr[i]; j++)
您还应该在使用它的 else 语句中声明变量标志。例如
for (int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
con++;
}
else if (arr[i] > 2)
{
bool flag = false;
//...
这就是问题所在 enter link description here
函数 prime 中的问题,但它认为是真的,我找不到解决方案
我在 codeforces 中提交了它,但它给了我。测试 5 的错误答案
:-
输入:
39
81 46 4 5 2 71 66 97 51 84 50 64 68 99 58 45 64 86 14 44 7 49 45 72 94 19 33 68 83 12 89 88 39 36 51 11 57 9 54
the wrong !!
#include <iostream>
#include <math.h>
using namespace std;
int maximum(int arr[], int n)
{
int max = INT_MIN;
for (int i = 0; i < n; i++)
{
if (max < arr[i]) { max = arr[i]; }
}
return max;
}
int minimum(int arr[], int n)
{
int min = INT_MAX;
for (int i = 0; i < n; i++)
{
if (min > arr[i]) { min = arr[i]; }
}
return min;
}
int prime(int arr[], int n)
{
int con = 0;
bool flag = true;
for (int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
con++;
}
else if (arr[i] > 2)
{
for (int j = 2; j < n; j++)
{
if (arr[i] % j == 0)
{
flag = false;
break;
}
else
{
flag = true;
}
}
if (flag == true)
con++;
}
}
return con;
}
int palindrome(int arr[], int n)
{
int i = 0, con = 0;
while (n--)
{
int temp;
temp = arr[i];
int reverseNumber = 0, rightDigit;
while (temp != 0)
{
rightDigit = temp % 10;
reverseNumber = (reverseNumber * 10) + rightDigit;
temp = temp / 10;
}
if (reverseNumber == arr[i]) {
con++;
}
i++;
}
return con;
}
int divisors(int arr[], int n)
{
int max = 0;
int con = 0;
int x = arr[0];
for (int i = 0; i < n; i++)
{
int temp = arr[i];
for (int j = 1; j <= arr[i]; j++)
{
if (arr[i] % j == 0)
{
con++;
}
}
if (max < con)
{
max = con;
x = arr[i];
}
else if (max == con)
{
if (x < arr[i])
{
x = arr[i];
}
}
con = 0;
}
return x;
}
int main()
{
int n; cin >> n;
int arr[1001];
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "The maximum number : " << maximum(arr, n) << endl;
cout << "The minimum number : " << minimum(arr, n) << endl;
cout << "The number of prime numbers : " << prime(arr, n) << endl;
cout << "The number of palindrome numbers : " << palindrome(arr, n) << endl;
cout << "The number that has the maximum number of divisors : " << divisors(arr, n) << endl;
divisors(arr, n);
return 0;
}
这个for循环
for (int j = 2; j < n; j++)
不正确。看来你的意思是
for (int j = 2; j < arr[i]; j++)
您还应该在使用它的 else 语句中声明变量标志。例如
for (int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
con++;
}
else if (arr[i] > 2)
{
bool flag = false;
//...