如何在 C++ 中的一行上打印一定数量的值?

How to print a certain number of values on one line in C++?

您好,我正在尝试打印从 1 到 4000 的所有质数,但我似乎无法弄清楚如何打印 8 个值然后创建一个新行。我快到了,但出于某种原因,我一直打印 8 个相同的值,但它确实打印了 8 个数字,然后跳到下一行。

让它打印 8 次相同的数字我哪里出错了?

我认为问题的主要逻辑出在isPrime()函数上。如果有人能帮助我,我将不胜感激。

bool isPrime(int number);
bool isPerfect(int number);

int main()
{

  // Print Prime Numbers
  cout << "PRIME NUMBERS: " << endl;

  int numberOfPrimes = 0, numberOfPerfects = 0;

  for (int i = 2; i < 4000; i++)
  {

      if(isPrime(i))
      {
          numberOfPrimes++;
      }
  }

  cout << endl << endl;

  cout << "PERFECT NUMBERS: " << endl;

  for (int j = 1; j <= 4000; j++)
  {
      if(isPerfect(j))
      {
          numberOfPerfects++;
      }
  }

  cout << endl << endl;
  cout << "The total number of primes form 1 to 4000 is " << numberOfPrimes << endl << endl;
  cout << "The total number of perfects from 1 to 4000 is " << numberOfPerfects << endl << endl;

}

bool isPrime(int number) {

    bool numberIsPrime = true;

    for (int i = 2; i * i <= number; i++)
    {
        if (number % i == 0)
        {
            numberIsPrime = false;
            return numberIsPrime;
            break;
        }

    }


     if (numberIsPrime) 
    {  
        int count = 0;
        for (int j = 0; j <= 7; j++)
        {
            count++;
            cout<< setw(6) << number << " ";

            if (count % 7 == 0)
            {
                cout << endl;
            }
        }
    }



    return numberIsPrime;

}

bool isPerfect(int number)
{
    int i = 1, sum = 0;
    bool numberIsPerfect = false;

    while( i < number)
    {
        if (number % i == 0)
        {
            sum = sum + i;
        }

        i++;
    }

    if (sum == number)
    {
        numberIsPerfect = true;
        cout << setw(6) << number << " ";
    }

    return numberIsPerfect;

}

输出

PRIME NUMBERS: 
 2      2      2      2      2      2      2 
 2      3      3      3      3      3      3      3 
 3      5      5      5      5      5      5      5 
 5      7      7      7      7      7      7      7 
 7     11     11     11     11     11     11     11 
11     13     13     13     13     13     13     13 
13     17     17     17     17     17     17     17 
17     19     19     19     19     19     19     19 
19     23     23     23     23     23     23     23 
23     29     29     29     29     29     29     29 
29     31     31     31     31     31     31     31 
31     37     37     37     37     37     37     37 
37     41     41     41     41     41     41     41 
41     43     43     43     43     43     43     43 
43     47     47     47     47     47     47     47 
47     53     53     53     53     53     53     53 
53     59     59     59     59     59     59     59 
59     61     61     61     61     61     61     61 
61     67     67     67     67     67     67     67 
67     71     71     71     71     71     71     71 
71     73     73     73     73     73     73     73 
73     79     79     79     79     79     79     79 
79     83     83     83     83     83     83     83 
83     89     89     89     89     89     89     89 
89     97     97     97     97     97     97     97 

它继续完成适当的素数但重复的值。

Where am I going wrong to have it print the same number 8 times?

在下面for;你打印 8 次相同的素数 number

    for (int j = 0; j <= 7; j++)
    {
        count++;
        cout<< setw(6) << number << " ";

我认为这是静态变量的作品

bool isPrime (int number)
 {
   static int count { 0 };

   for ( int i { 2 } ; i * i <= number ; ++i )
      if ( number % i == 0 )
         return false;

   cout << setw(6) << number << " ";

   if ( ++count == 8 )
    {
      cout << endl;

      count = 0;
    }

   return true;
 }

或(恕我直言好一点)

bool isPrime (int number)
 {
   static int count { 0 };

   bool isP { true };

   if ( number == 2 ) // 2 is prime !
      ;
   else if ( 0 == (number & 1) ) // is divisible by 2 ?
      isP = false;
   else
    {
      int sq = std::sqrt(number); // calculare square root only one time

      // try dividing by odd number only
      for ( int i { 3 } ; i <= sq ; i += 2 ) 
         if ( number % i == 0 )
            isP = false;
    }

   if ( isP )
    {
      std::cout << setw(6) << number << " ";

      if ( 0 == (++count & 7) ) // is divisible by 8 ?
         std::cout << std::endl;
    }

   return isP;
 }

您的代码结构不佳。打印任何东西不是 isPrime() 的工作。如果参数是质数,isPrime() 应该 return 为真, 什么都不做。 然后 在调用代码中,如果是素数,你应该打印数字,计算到目前为止你在该行中打印的素​​数的数量,当它达到 8 或每行你想要的数量时,打破这一行并将计数归零。

事实上,即使 一个 isPrime() 方法也是不好的做法。通过 Eratosthenes 筛法,通过 Eratosthenes 筛法一次性生成该范围内的所有素数,然后 然后 打印它们,而不是询问每个数字,效率要高出几个数量级素数的顺序范围。