题解 | #判断一个数是不是质数#C++/while

判断一个数是不是质数

https://www.nowcoder.com/practice/b8bb5e7703da4a83ac7754c0f3d45a82

#include <iostream>
using namespace std;

int main() {
    int number;
    cin >> number;
    if (number > 1) {       //这个是输入的条件
        int i = 2;         //1不用判断,判断1以后的质数
        while (i <=
                number) {         //就是我用需要判断的数去不断除2--number这个范围的数直到找到一个因数可以除尽
            if (number % i ==
                    0 ) {                //求余数等于0说明这个i是它的一个因数
                if (i != number) {
                    i = number +
                        1;            //让i变大以后使得循环趋假,离开循环,不需要再去找它的因子了,只用找到一个足够了
                    cout << "不是质数" << endl;

                } 
			   else
                    cout << "是质数" << endl;
            }
            i++;
        }

    } 
    else
        cout << "不合法" << endl;


    return 0;
}

全部评论

相关推荐

路过的周先森:我就喜欢这种无限复活+筛选快的,比那些投进去就没声还占用投递次数的好多了
投递快手等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务