zoukankan      html  css  js  c++  java
  • ural 1086. Cryptography

    1086. Cryptography

    Time Limit: 2.0 second
    Memory Limit: 64 MB
    While preparing this problem set the jury has run into the following problem: it was necessary to send by e-mail the texts of the problems. As it is well known, e-mail is not reliable, messages are sent not enciphered, there is a danger that someone can intercept them. The members of the program committee wanted no participant know the texts of the problems before the start of the contest. That's why they resorted to cryptography methods in order to save the texts of the problems from an unsanctioned reading. The jury gas worked up a new way of enciphering of a text. It is not patented yet, so it's kept secret. However, we'll reveal you one secret: the new algorithm is based on the work with prime numbers. In particular, in uses a calculation of n-th by order prime number.
    Several members of the program committee independently have worked up programs that make such calculations, but these programs produce different answers. Each one of the programmers is sure that his program works correctly. That's why the jury has reached the deadlock and can't continue working. The contest is about not to take place.
    You are to help to the jury and to save the contest. We want you to write a program that calculates the n-th by order prime number. The main thing is that your program should work correctly.

    Input

    First line contains a positive integer k. Then k positive integers follow (one in each line). The numbers don't exceed 15000.

    Output

    For each number n you should output the n-th by order prime number. Each number should be in its line.

    Sample

    inputoutput
    4
    3
    2
    5
    7
    
    5
    3
    11
    17
    

    Hint

    The prime number is a positive integer that has exactly two different positive divisors, i.e. 1 is not a prime number.
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cmath>
     4 #define MAX 15000+10
     5 using namespace std;
     6 
     7 int prime_num[MAX]={1};
     8 
     9 void init(){
    10     int k = 2,i = 3;
    11     prime_num[1] = 2; //test #2中注意可能要求输入1,输出应该是2,不是1
    12     while(k < MAX){
    13         for(;; i += 2){
    14             int j,m =(int)sqrt(i)+1 ;
    15             for(j = 2; j < m; j ++ ){
    16                 if(i%j == 0) break;
    17             }
    18             if(j >= m ) prime_num[k++]=i;
    19             if(k >= MAX) break;
    20         }
    21     }
    22 }
    23 
    24 int main(){
    25     init();
    26     int n,a;
    27     cin >> n ;
    28     for(int i = 1; i <= n; i ++ ){
    29         cin >> a;
    30         cout<<prime_num[a]<<endl;
    31     }
    32 
    33     return 0;
    34 }
  • 相关阅读:
    flask和tornado的区别
    tornado1
    人脸识别
    ocr:img转成pdf,pdf转成pdf
    flask2
    《个人-GIT使用方法》
    课后作业-阅读任务-阅读笔记
    课后作业-阅读任务-阅读提问
    结对-四则运算生成器-设计文档
    团队-手机备忘录-开发环境搭建过程
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3034879.html
Copyright © 2011-2022 走看看