如何在C ++中显示最大的素数? [英] How to I display the largest prime number in C++?

查看:94
本文介绍了如何在C ++中显示最大的素数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何显示最大素数,有人可以帮帮我吗?我已经能够显示素数,但我只需要显示最大值。



应该可以做到这一点但是我已经被困了几个星期了:



一个程序要求用户输入一系列数字(使用0来停止系列。输入所有数字后,程序应显示系列中最大的素数。数据验证:数字应大于0。 />


我尝试了什么:



I don't know how to display the largest prime number, can someone help me? I've been able to show the prime numbers but I need to only show the largest.

it should be able to do this but ive been stuck for weeks now :

A program that ask the user to enter a series of numbers (use 0 to stop the series. After all the numbers have been entered, the program should display the the largest prime number of the series. Data validation: numbers should be greater than 0.

What I have tried:

#include <iostream>
#include<cfloat>
using namespace std;
int main() {
    int num,i,flag,n;
    cin>>n;
    cout<<"The Prime Numbers Are: ";
    for(num=1; num<=n; num++){
        flag=0;
        for(i=2;i<=num/2;i++){
            if(num%i==0){
                flag=1;
                break;
            }
        }
        if(flag==0 && num!=1)
            cout<<num<<endl;
    }
return 0;
}

推荐答案

首先让用户输入数字并存储它们。用户按降序排列的数字排序 - 然后依次处理每个数字。从最大的开始。

依次检查每个数字,并查明它是否为素数。如果是,打印并退出。如果不是,请转到下一个。



替代方法是保持最大值,并且当用户输入每个值时检查它是否大于那。如果是,请检查它是否为素数 - 如果不是忽略它。如果是,则将最大值设置为新值,然后转到下一个值。当用户输入0时,打印最大或者说没有一个是素数的消息。



但这是你的功课,所以我不会给你代码!
Start by getting the user to enter the numbers, and store them. The sort the numbers the user entered in descending order - then process each in turn, starting withe the largest.
Check each number in turn, and find out if it is prime. if it is, print it and exit. if it isn't, move to the next.

The alternative is to keep a "largest" value, and as the user enters each value check if it's bigger than that. If it is, check if it's prime - if it isn't ignore it. If it is, set largest to the new value, and go round for the next value. when the user enters 0, print largest or a message saying "none of them was prime".

But this is your homework, so I'll give you no code!


首先,你必须开发一些概念,例如OriginalGriff编写的。最好是保持输入,但在矢量或数组中按原始顺序。接下来,您需要编写一些检查引物号的函数。素数的结果,我会存储排序。



提示:使用中断标准0进行输入的while循环。
At first you must develop some concept, for instance as OriginalGriff has written. Best is to keep the input, but in original order in a vector or array. Next you need to code some function which checks for a primer number. The result of prime numbers, I would stored sorted.

Tip: make a while loop for input with break criteria 0.


正如Griff建议您可以动态计算最大的素数,而不记录所有用户输入。首先,将素性测试放在一个单独的函数上,例如

As Griff suggested you may compute the largest prime number on the fly, without recording all user inputs. To start, put the primality test on a separate function, e.g.
bool is_prime(int n)
{
  for (int i=2; i<n; ++i)
    if ( n % i == 0)
      return false;

  return true;
}





然后

  • 将任意无效值分配给 max_prime (例如 0 )。


  • Then

    • Assign an arbitrary, invalid value to max_prime (e.g. 0).
      • 实现用户输入循环:如果用户输入的数字大于 max_prime 它是素数,则更新 max_prime value。
      • Implement the user input loop: if the number the user enters is bigger than max_prime and it is prime then update max_prime value.
      • 循环完成检查 max_prime 有效且可能会报告。
      • On loop completed check if max_prime is valid and possibly report it.


      这篇关于如何在C ++中显示最大的素数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆