zoukankan      html  css  js  c++  java
  • 判断素数

    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;
    }
    */
  • 相关阅读:
    Linux私房菜——防火墙部分笔记
    ubuntu
    序列求和
    圆的面积
    Fibonacci数列
    JavaScript中定义数组
    C语言中的EOF
    jQuery获取的值去掉px
    jQuery中单引号和双引号的使用
    jQuery报错:Uncaught ReferenceError: $ is not defined
  • 原文地址:https://www.cnblogs.com/cbhhh/p/6013897.html
Copyright © 2011-2022 走看看