蓝桥杯_算法训练_Torry的困惑(基本型)

这个题目就是求质数的乘积,在加一个模,思路比较简单,直接上代码:

 1 #include<iostream>
 2 using namespace std;
 3 bool isPrime(int a)
 4 {
 5     int flag = 0;
 6     for(int i = 2; i < a; i++)
 7     {
 8         if(a%i==0)
 9         {
10             flag = 1;
11             return false;    
12         }
13     } 
14     if(flag==0)
15     {
16     //    cout<<a<<" ";
17         return true;
18     }
19 }
20 int main()
21 {
22     long n;
23     long a = 2;//当前的数字 
24     long num = 0;//记录有多少个质数 
25     long result = 1;//记录结果 
26     cin>>n;
27     while(1)
28     {
29         if(num>=n)    break;
30         else
31         {
32             if(isPrime(a))
33             {
34                 num++;
35                 result = result*a;
36             }
37             a++;
38         }
39     } 
40     cout<<result%50000<<endl;
41     return 0;
42 }

 

posted on 2017-08-16 11:51  宋田茹  阅读(306)  评论(0编辑  收藏  举报