输入一个数,判断其是否为素数;

本题有多组测试样例。

输入规则如下:

第一行为一个整数,样例组数T;

第二至第t+1行每行都有一个整数a(a >= 2),表示需要处理的数;

如果是素数则输出“yes”,否则输出“no”,每个样例的输出占1行;

输入输出样例:

输入:

2

99

17

输出:

no

yes

就是判断素数问题,本来我是想用sqrt的,但是不知道为什么过不了,所以就用了\2,本来用前者是能够减少时间的,不过检测数据小,都没有关系

#include<stdio.h>
int main() {
int t, a, flag = 1;
scanf("%d", &t);
while (t--) {
scanf("%d", &a);
for (int i = 2; i < a/2; i++) {
if (a % i == 0) {
printf("no\n");
flag = 0;
break;
}
}
if (flag == 1) {
printf("yes\n");
}
flag = 1;
}
return 0;
}

  

标程

1.#include <stdio.h>
2.
3.int main()
4.{
5. int t, a, i;
6. scanf("%d", &t);
7. for (; t > 0; t--)
8. {
9. scanf("%d", &a);
10. for (i = 2; i <= a/2; i++)
11. {
12. if (a % i == 0) break;
13. }
14. if (i > a/2) printf("yes\n");
15. else printf("no\n");
16. }
17. return 0;
18.}

  

[1002]prime的更多相关文章

  1. 1002: Prime Path

    题目链接:http://172.16.200.33/JudgeOnline/problem.php?id=1002 题意:给你两个四位数的素数,求最少经过多少步的变化能够从一个素数变到另一个素数.在变 ...

  2. hdu 1002 prime 模板

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. Lightoj 1002 - Country Roads(prim算法)

    I am going to my home. There are many cities and many bi-directional roads between them. The cities ...

  4. POJ 3126:Prime Path

    Prime Path Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit St ...

  5. 2021“MINIEYE杯”中国大学生算法设计超级联赛(8)(1002,1004,1006,1009)

    前言 依旧是白嫖账号,只打了一些题/kk 正题 1002 Buying Snacks 题目大意 \(n\)个物品,每个可以买一次也可以不买,如果买需要选择\(1/2\)块钱的,然后也可以相邻两个一起买 ...

  6. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  7. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  8. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  9. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

随机推荐

  1. cefsharp在xp上运行

    今天遇到一个坑.也是自己英语不足的体现.在xp上运行cefsharp.wpf. 查询了各种资料.按照说明一步一步的操作,都没有解决xp上运行cefsharp.wpf. 而且在xp上调试都不知道错误在哪 ...

  2. php单独编译扩展模块

    以pdo_mysql为例: 1.下载 文件 或者 进入 在PHP源码包中进入ext/pdo_mysql http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz 2.解压 ...

  3. windows下调用发送邮件程序项*发送邮件

    #include <windows.h>int _tmain(int argc, _TCHAR* argv[]){ ShellExecute(NULL, _T("open&quo ...

  4. 沉浸式状态栏_boolean hasTopLine = a.getBoolean(1, false);//AS会在"1"下显示错误红线

    TypedArray a = mContext.obtainStyledAttributes(attrs); boolean hasBottomLine = a.getBoolean(0, false ...

  5. dp、px、dpi、ppi

    概念: dpi(Dots Per Inch):每英寸上的点数,最初用于衡量打印物上每英寸的点数密度,打印机在一英寸内打多少个点.DPI值越小越不精细. ppi(Pixels Per Inch):每英寸 ...

  6. [mk] 喝一杯咖啡, 写一写 Makefile

    Makefile 是 Linux 下组织程序的一个工具,它的命令是 make. (首字母M/m都可以) [Makefile] Makefile 编写的主旋律: target: [dependency] ...

  7. git使用--git命令项目提交问题总结

    提交遇到Error  "remote ref does not exist"解决办法:git fetch -p MY_REMOTE    eg.    git fetch -p o ...

  8. Java中抽象类和接口的区别

    转载自:http://dev.yesky.com/436/7581936.shtml 在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种 ...

  9. ORA-12543: TNS:destination host unreachable

    在连接Oracle数据库时,如果使用Tnsnames.ora中配置的数据源名称有时会报 ORA-12543: TNS:destination host unreachable 异常,比如:在Tnsna ...

  10. Java EE 和 Java Web

    什么是 Java Web 应用程序? Java Web 应用程序会生成包含各种类型的标记语言(HTML 和 XML 等)和动态内容的交互式 Web 页.它通常由 Web 组件组成(如 JavaServ ...