这两天在准备保送到交大软院,由于要考机试,这两天狂练C++中,把自己的代码贴出来吧,不能再懒了,再懒博客就要荒废了。
题目:
Problem A. Prime Number
Input file: Standard Input Page 1 of 2
Output file: Standard Output
Time Limit: 1 Second
Output the k-th prime number.
Input
k≤10000
Output
The k-th prime number.
Sample input and output
Standard Input Standard Output
3 5
7 17
C++代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> #include <cmath> using namespace std; int isPrime(int n) { int i,flag = 1; for (i=2;i<=sqrt(n);++i) { if (n % i == 0) { flag = 0; break; } } return flag; } int main() { int i = 1,n = 2,k,result; cin >> k; while(i <= k) { result = n++; if (isPrime(result)) ++i; } cout << result; } |
