九度OJ—题目1047:素数判定

题目描述:

给定一个数n,要求判断其是否为素数(0,1,负数都是非素数)。

输入:

测试数据有多组,每组输入一个数n。

输出:

对于每组输入,若是素数则输出yes,否则输入no。

样例输入:
13
样例输出:
yes
#include <iostream>
#include<stdio.h>
#include<math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool isprime(int x)
{
    if(x<=1)return false;
    else
    {
        int bound=(int)sqrt(x)+1;
        for(int i=2;i<=bound;i++)
        {
            if(x%i==0)return false;
        }
        return true;
    }
}
int main(int argc, char** argv) {
    int x;
    while(scanf("%d",&x)!=EOF)
    {
        puts(isprime(x)?"yes":"no");
    }
    return 0;
}

九度OJ 1163 素数

题目描述:

输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数,如果没有则输出-1。

输入:

输入有多组数据。每组一行,输入n。

输出:

输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数(素数之间用空格隔开,最后一个素数后面没有空格),如果没有则输出-1。

样例输入:
100
样例输出:
11 31 41 61 71
来源:
2008年北京航空航天大学计算机研究生机试真题

#include <iostream>
#include<stdio.h>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int prime[10000];
int primesize=0;
bool isprime[10000];
void init()
{
for(int i=0;i<=10000;i++)
{
isprime[i]=true;
}
for(int i=2;i<=10000;i++)
{
if(isprime[i]==false)continue;
prime[primesize++]=i;
for(int j=i+i;j<=10000;j+=i)
{
isprime[j]=false;
}
}
}
int main(int argc, char** argv) {
int n;
while(scanf("%d",&n)!=EOF)
{
init();
bool isfirst=true;
for(int i=0;i<primesize;i++)
{
if(prime[i]<n&&prime[i]%10==1)
{
if(isfirst)
{
printf("%d",prime[i]);
isfirst=false;
}
else
{
printf(" %d",prime[i]);
}
}
}
if(isfirst)
printf("-1\n");
else
{
printf("\n");
}
}
return 0;
}

九度OJ 1040 Prime Number (筛素数,试除法)

题目描述:

Output the k-th prime number.

输入:

k≤10000

输出:

The k-th prime number.

样例输入:
3
7
样例输出:
5
17
#include <iostream>
#include<stdio.h>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int prime[10000];
int primesize=0;
bool isprime[10000];void init()
{
    for(int i=2;i<=10000;i++)
    {
        isprime[i]=true;
    }
    for(int i=2;i<=10000;i++)
    {
        if(isprime[i]==false)continue;
        prime[primesize++]=i;
        for(int j=i+i;j<=10000;j+=i)
        {
            isprime[j]=false;
        }
    }
}int main(int argc, char** argv) {
    int n;
    init();
    while(scanf("%d",&n)!=EOF)
    {
        printf("%d\n",prime[n-1]);
    }
    return 0;
}

【九度】题目1440:Goldbach's Conjecture 2

题目描述:

Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.

输入:

An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.

输出:

Each output line should contain an integer number. No other characters should appear in the output.

样例输入:
6
10
12
0
样例输出:
1
2

1

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool isprime(int x)
{
if(x<=1)return false;
else
{
int bound=(int)sqrt(x)+1;
for(int i=2;i<=bound;i++)
{
if(x%i==0)return false;
}
return true;
}
}
int main(int argc, char** argv) {
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
int hash[10000]={0};
int count=0;
for(int i=2;i<n;i++)
{
int b=n-i;
if(hash[i]==1||hash[b]==1)continue;
else
{
hash[i]=1;
if(isprime(i)&&isprime(b))
{
count++;
}
}
}
printf("%d\n",count);
}
return 0;
}

九度OJ 1087 约数的个数

题目描述:

输入n个整数,依次输出每个数的约数的个数

输入:

输入的第一行为N,即数组的个数(N<=1000)
接下来的1行包括N个整数,其中每个数的范围为(1<=Num<=1000000000)
当N=0时输入结束。

输出:

可能有多组输入数据,对于每组输入数据,
输出N行,其中每一行对应上面的一个数的约数的个数。

样例输入:
5
1 3 4 6 12
样例输出:
1
2
3
4
6
人家的http://blog.csdn.net/jdplus/article/details/18353667
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int main(void){
  5. int N;
  6. int * data = NULL;
  7. int i, j;
  8. int cnt, tmp;
  9. while (scanf("%d", &N) == 1 && N != 0){
  10. data = (int *)malloc(sizeof(int) * N);
  11. if (data == NULL)
  12. break;
  13. for (i=0; i<N; ++i)
  14. scanf("%d", &data[i]);
  15. for (i=0; i<N; ++i){
  16. cnt = 0;
  17. for (j=1; j<=(int)sqrt(data[i]*1.0); ++j){
  18. if (data[i] % j == 0){
  19. cnt = cnt + 2;
  20. }
  21. }
  22. tmp = (int)(sqrt(data[i]*1.0));
  23. if (tmp * tmp == data[i])
  24. --cnt;
  25. printf("%d\n", cnt);
  26. }
  27. free(data);
  28. }
  29. return 0;
  30. }
我的,说实话真不知道错在哪里呀
#include<stdio.h>
#include<string.h>
#include<math.h>
long long prime[100001];
long long primesize;
bool mark[100001];
void init()
{
    long long bound=(long long)sqrt((pow(10,9)))+1;
    for(long long i=2;i<=bound;i++)
    {
        mark[i]=true;
    }
    for(long long i=2;i<=bound;i++)
    {
        if(mark[i]==false)continue;
        prime[primesize++]=i;
        for(long long j=i*i;j<=bound;j+=i)
        {
            mark[j]=false;
        }
    }
}
int main()
{
    init();
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            long long tas;
            long long num=1,tmp;
            scanf("%lld",&tas);
            for(long long j=0;j<primesize;j++)
            {
                tmp=1;
                if(tas==1)continue;
                while(tas%prime[j]==0)
                {
                    tmp++;
                    tas/=prime[j];
                }
                num*=tmp;
            }
            if(tas!=1)
            {
                num+=2;
            }
            printf("%lld\n",num);
        }
    }
}

这题我做得比较麻烦,但是看人家做的见质数+2居然ac通过了,我就纳闷,这应该是错的呀。

好吧,我明白了,我居然明白了,这么抽象!!!+2是和前面n!求的逻辑一样。

九度1047 1163相关推荐

  1. 九度OJ 1163 素数

    题目地址:http://ac.jobdu.com/problem.php?pid=1163 题目描述: 输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和 ...

  2. 九度 1408 寻找表达式 (中缀转后缀)

    题目描述 总结 1. '_' 运算符不是 a*10 + b, 而是 a*(10 or 100) + b 2. char * 与 string 的相互转化 char* = string.c_str() ...

  3. 剑指Offer - 九度1511 - 从尾到头打印链表

    剑指Offer - 九度1511 - 从尾到头打印链表2013-11-29 21:08 题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例. 每一组测试案 ...

  4. 九度 1545:奇怪的连通图

    题目描述 总结 1. 用 BFS 实现 Dijkstra. 要点是, visited 后标记, 把某个点从优先队列取出后再标记 代码 未通过九度测试 RE /** source.cpp** Creat ...

  5. 九度题库(所有题目整理,适合计算机考研和面试的人用)

    本来搜一道面试题,找到叫九度题库的地方,发现里面的题目都比较基础,很适合当面试题来练习. 于是,闲得蛋疼,把所有题目给爬下来了,并整理成markdown格式,然后export成pdf,方便大家离线阅读 ...

  6. 九度 1462:两船载物问题(01背包)

    题目描述: 给定n个物品的重量和两艘载重量分别为c1和c2的船,问能否用这两艘船装下所有的物品. 思路 1. 朴素背包问题 2. 有几个细节要好好把握 (1) 在读入物品重量时顺带统计物品的最大值和总 ...

  7. 【强烈推荐】程序猿们,九度Online Judge开始举办月赛啦!!会编程才是王道!!!!!

    程序猿们,九度Online Judge开始举办月赛啦!!会编程才是王道!! 在国内计算机考研中,已经有越来越多的高校采用ACM上机考试的形式,在复试中来考察考生的实际动手编程能力,并且机试在复试中所占 ...

  8. 剑指Offer - 九度1367 - 二叉搜索树的后序遍历序列

    剑指Offer - 九度1367 - 二叉搜索树的后序遍历序列2013-11-23 03:16 题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出 ...

  9. 九度OJ1486 /POJ 1029/2012北京大学研究生复试上机

    wa到死!wa到死!这是一个看着简单,坑及其多的题! 坑一:POJ上是单组输入,九度上是多组输入,妈蛋要是研究生复试遇到这种大坑肯定死掉啊!而且对于codeforces比较习惯的 同学肯定会觉得巨坑无 ...

最新文章

  1. sql server日期时间函数
  2. 应该知道的自动化测试陷阱2
  3. php 进制 小数,小数进制转换
  4. 用ajax(vb.net) 实现dropdownlist二级无刷新联动~!
  5. 对tmemorystream的一些改进_delphi教程 [转]
  6. php.ini 是否设置路由,php – 如何在路由INI文件中为Zend Framework中的子域编写路由链?...
  7. Win11右键菜单切回经典模式
  8. 通过Java技术手段,某程序员发现自己被绿了!
  9. 免疫算法(matlab)
  10. Windows 好用的护眼软件
  11. 个人独资有限公司章程模板
  12. “十步杀一人,千里不留行。事了拂衣去,深藏功与名。”
  13. umail for linux,umail for linux邮件服务器备份与还原
  14. 林大计算机科学考研分数线,2018年北京林业大学考研复试分数线已公布
  15. Tent-Logistic-Cosine混沌映射(提供参考文献及Matlab代码)
  16. python sobel算子_python自编程序实现——robert算子、sobel算子、Laplace算子进行图像边缘提取...
  17. 北方工业大学计算机考研资料汇总
  18. 有孚原力超算,为客户提供定制化高性能计算服务
  19. 华为特聘PPT设计师年薪百万:改变你的不是打卡,而是坚持
  20. GPLT L2-040 哲哲打游戏

热门文章

  1. wpsppt插入html,WPS的做好的一个PPT 怎么插入到另一个PPT?
  2. 计算机学院认知实习报告
  3. zabbix微信告警(虚拟机脚本测试成功,zabbix上收不到信息)
  4. 博尔赫斯-诗中的经典语段
  5. 毕业设计-基于 MATLAB 的车牌识别系统设计
  6. 获取图像的Hu不变矩
  7. Ubuntu-拼音输入法安装
  8. 10款idea神级插件,生产力必备神器!
  9. 纸上得来终觉浅,绝知此事要躬行
  10. VB创建写字板小程序