【问题标题】:program to find The number of prime numbers in array using cpp使用cpp查找数组中素数的程序
【发布时间】:2021-11-14 15:05:01
【问题描述】:

这就是问题 enter link description here

函数中的问题,但它认为是真的,我找不到解决方案

我在 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;
}

【问题讨论】:

  • 发现程序没有产生正确的结果后你做了什么?正确的做法是实际调试它。在调试器中运行程序和/或添加调试打印语句以跟踪程序执行。 How to debug small problems

标签: c++ arrays for-loop primes function-definition


【解决方案1】:

这个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;
        //...

【讨论】:

  • 成功了,谢谢你 :) :)
  • @MostafaMohamedTaha 如果问题得到解决,那么您可以选择最佳答案来关闭问题。在这种情况下,您的声誉将会提高。:)
猜你喜欢
  • 2016-03-08
  • 2021-06-05
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多