zoukankan      html  css  js  c++  java
  • 判素数

                                                   素数判定
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    Description

    对于表达式n 2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x,y<=50),判定该表达式的值是否都为素数。

    Input

    输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

    Output

    对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。

    Sample Input

    0 1
    0 0

    Sample Output

    OK

     //主要是想说一下sqrt(x)精度损失问题。
    //floor向下取整(包含在assert.h头文件中)。
    1
    #include<stdio.h> 2 #include<string.h> 3 #include<assert.h> 4 #include<math.h> 5 6 int judge(int x)//判断一个数是否为素数//logn 7 { 8 if(x==0 || x==1) return 0; 9 int m = floor(sqrt(x)+0.5); 10 for(int i=2; i<=m; i++) 11 { 12 if(x%i==0) 13 { 14 return 0; 15 } 16 } 17 return 1; 18 } 19 int main() 20 { 21 int x, y, i, k, cnt; 22 int s[10000]; 23 while(~scanf("%d%d", &x, &y) ) 24 { 25 if(x==0 && y==0) break; 26 k = 0; 27 cnt = 0; 28 memset(s, 0, sizeof(s)); 29 for(i=x; i<=y; i++) 30 { 31 s[k++] = i*i+i+41; 32 if(s[k-1]<0) 33 s[k-1] = -s[k-1]; 34 if(judge(s[k-1])) 35 cnt++; 36 } 37 if(cnt == y-x+1) 38 printf("OK "); 39 else printf("Sorry "); 40 } 41 return 0; 42 }
    每天训练发现我比别人做的好慢,但是理解的更深刻,如果一开始学一个新知识点就搜模板,那么这样的人是走不远的,毕业之后带走的只有思维,什么荣誉,奖杯都已经不重要了。
  • 相关阅读:
    redis基本操作 —— hash
    redis基本操作 —— string
    redis —— linux下源码安装
    zookeeper c api 安装 & 连接 zookeeper
    wpa_supplicant移植(2.9版本)
    hostapd移植(2.6版本为例)
    hostapd移植(2.7版本)
    使用MKdocs搭建个人主页并关联到GithubPages上
    yolov5的yaml文件解析
    RANSAC——(RANdom SAmple Consensus(随机抽样一致))
  • 原文地址:https://www.cnblogs.com/6bing/p/4115665.html
Copyright © 2011-2022 走看看