文章标题
来源:互联网 发布:xml json弹幕 编辑:程序博客网 时间:2024/06/05 06:57
Primes
Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes.
Input
Each input line contains a single integer. The list of integers is terminated with a number<= 0. You may assume that the input contains at most 250 numbers and each number is less than or equal to 16000.
Output
The output should consists of one line for every number, where each line first lists the problem number, followed by a colon and space, followed by “yes” or “no”.
Sample Input
1
2
3
4
5
17
0
Sample Output
1: no
2: no
3: yes
4: no
5: yes
6: yes
- 题解
简单的判断一个数是否为素数问题,注意此题2竟然不是素数,真不知道出题人怎么想的,最后注意的就是输出格式问题。下面附上代码段,希望各位大佬能够提供更好的解题思路。
#include<stdio.h>#include<math.h>int main(void){ int n; int m; int i = 2; int test = 0; while(scanf("%d",&n)!=EOF) { m = (int)sqrt(n); if(n<=0) break; ++test; for(i = 2;i <= m; i++) if(n % i == 0) break; if(n==1||n==2) printf("%d: %s\n",test,"no"); else if(i > m) printf("%d: %s\n",test,"yes"); else printf("%d: %s\n",test,"no"); } return 0;}
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- FI功能增强业务说明书
- Oracle 11g 移动数据文件路径
- linux修改python解释器的默认编码模式
- 加入CSDN
- oracle导入数据步骤
- 文章标题
- VB Select Case
- 内部类
- 《Spring + Cloud微服务实战》读书笔记(一)————基础知识
- Vue.js状态管理工具Vuex快速上手
- 二叉树的定义,性质及存储
- Ubuntu_DNS配置过程记录
- 先知创新大会:16位华人安全科学家分享前沿研究,中国密码专家王小云获先知奖
- 数据库优化