NYOJ-169 primes

Source: Internet
Author: User

Prime Number
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 1
Describe
Entering the Expo Park, an information and communication museum, visitors will receive an unprecedented experience of the most sophisticated interactive, a full of creative and joyful information and communication interactive experience show will be presented in a new form, from the audience step into the pavilion from the first step, will be inseparable from the handheld terminal, the future of human dreams from the visitor's pocket unfold.

In the dream garden of the waiting area, visitors begin their magical journey of experience, with the use of terminals such as mobile phones to participate in interactive games with the virtual character KR in the Dream Theatre. Kong for guessing games. When the screen appears with an integer x, if you can compare the KR. The Kong faster sends out the nearest prime number answer, and you will get an unexpected gift.

For example: When the screen appears 22 o'clock, your answer should be 23; When the screen appears 8 o'clock, your answer should be 7; if x itself is a prime number, answer X; If the number of primes closest to X is two, the number is greater than its prime.

Input
First line: N the number of integers to guess
Next there are n rows with a positive integer x per line
1<=n<=5 1<=x<=1000
Output
The output has n rows, and each row is the nearest prime number corresponding to X
Sample input
4
22
5
18
8
Sample output
23
5
19
7
Source
The third Henan Province Program design Competition

The main topic: to find the number of the nearest prime, specific requirements see the topic.

My side. The use of the number of primes is the method of determining the number of twins, see: http://blog.csdn.net/code_pang/article/details/7880245

#include <iostream>#include <cstdio>using namespace STD;BOOLIsPrime (intNUM) {if(num==1)return false;if(num==2|| num==3)return true;if(num%6!=1&& num%6!=5)return false; for(intI=5; i*i<=num;i+=6)if(num%i==0|| num% (i+2)==0)return false;return true;}intMain () {intTscanf("%d", &t); while(t--) {intNscanf("%d", &n);if(IsPrime (n)) {cout<<n<<endl;Continue; }intAnt=0; for(intj=1;j< -; j + +) {if(IsPrime (N+J)) {if(IsPrime (N-J)) {cout<<n+j<<endl; Ant=1; }Else{cout<<n+j<<endl; Ant=1; }            }Else if(IsPrime (N-J)) {cout<<n-j<<endl; Ant=1; }if(ant==1) Break; }    }return 0;}

Only on behalf of personal views, welcome to Exchange discussion, do not spray ~ ~

Photoby:wlop

Http://weibo.com/wlop

NYOJ-169 primes

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.