zoukankan      html  css  js  c++  java
  • HDU 2161 Primes

    http://acm.hdu.edu.cn/showproblem.php?pid=2161

    Problem Description
    Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes. 
     
    Input
    Each input line contains a single integer. The list of integers is terminated with a number<= 0. You may assume that the input contains at most 250 numbers and each number is less than or equal to 16000.
     
    Output
    The output should consists of one line for every number, where each line first lists the problem number, followed by a colon and space, followed by "yes" or "no". 
     
    Sample Input
    1
    2
    3
    4
    5
    17
    0
     
    Sample Output
    1: no
    2: no
    3: yes
    4: no
    5: yes
    6: yes
     
    时间复杂度:$O(sqrt{N})$
    代码:
    #include <bits/stdc++.h>
    using namespace std;
    
    int N;
    
    int prime(int x) {
        for(int i = 2; i * i <= x; i ++) {
            if(x % i == 0)
                return 0;
        }
        return 1;
    }
    
    int main() {
        int Case = 1;
        while(~scanf("%d", &N)) {
            if( N <= 0) break;
            printf("%d: ", Case ++);
            if(N == 1 || N == 2)
                printf("no
    ");
    
            else {
                 if(prime(N))
                    printf("yes
    ");
                else
                    printf("no
    ");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    JDBC第一部分
    java mysql学习第三部分
    java mysql 第六部分
    java mysql学习第五部分
    java mysql学习第二部分
    java mysql学习第一部分
    元注解
    改良之前写的模拟栈代码
    java中如何自定义异常
    java中的语法规则
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9637360.html
Copyright © 2011-2022 走看看