• 判断素数


    Description

    这是一个很经典也很简单的小题目,就是判断一个给定的正整数是否素数。

    Input

    有多组测试样例,每组输入在第一行给出一个正整数N(<=10),随后N行,每行给出一个小于2的31次的需要判断的正整数。

    Output

    对每个需要判断的正整数,如果它是素数,则在一行中输出“Yes”,否则输出“No”。

    Sample Input

    2 11 111

    Sample Output

    Yes No

     

    一道很基础的题目

    但是由于我最开始忽略了判断数为1时 进不了while循环 导致将1判断为素数的错误 一直不能ac

    注释部分代码为错误代码 

    #include <iostream>
    using namespace std;
    int main(){
        int n;
        int m;
        int i;
        
    loop:while(cin>>n){
        while(n--){
            cin>>m;
            if(m<2)cout<<"No"<<endl;
            else{
                for(i=2;i<=m/2;i++)
                    if(m%i==0)
                    {cout<<"No"<<endl;
                        goto loop;
                    }
                cout<<"Yes"<<endl;
            }
        }
    }
        return 0;
    }
    
    
    /*
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        int n;
        int i;
        bool j = true;
        int x;
        cin >> x;
        while(x--)
        {
            
            cin>>n;
            i = 2;
            while (i < n)
            {
                if (n % i == 0)
                {
                    
                    j = false;
                }
                i++;
            }
            
            if (j == true)
                cout<<"Yes"<<endl;
            else 
                cout<<"No"<<endl;
        }
        return 0;
    }
    */
  • 相关阅读:
    REUSE_ALV_GRID_DISPLAY_LVC I_CALLBACK_HTML_TOP_OF_PAGE
    查找数组中最大值java
    jvm 调优
    jvm 内存
    树形遍历文件夹
    程序创建一个ArrayList,添加1到10的10个随机数,删除大于5的数 java
    字符串反序排序 并带有空格输出 java
    摆动排序
    免密登陆
    springboot UEditor集成
  • 原文地址:https://www.cnblogs.com/cbhhh/p/6013897.html
Copyright © 2020-2023  润新知