九度1047 1163

九度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.    
  5. int main(void){  
  6.     int N;  
  7.     int * data = NULL;  
  8.     int i, j;  
  9.     int cnt, tmp;  
  10.    
  11.     while (scanf("%d", &N) == 1 && N != 0){  
  12.         data = (int *)malloc(sizeof(int) * N);  
  13.         if (data == NULL)  
  14.             break;  
  15.         for (i=0; i<N; ++i)  
  16.             scanf("%d", &data[i]);  
  17.         for (i=0; i<N; ++i){  
  18.             cnt = 0;  
  19.             for (j=1; j<=(int)sqrt(data[i]*1.0); ++j){  
  20.                 if (data[i] % j == 0){  
  21.                     cnt = cnt + 2;  
  22.                 }  
  23.             }  
  24.             tmp = (int)(sqrt(data[i]*1.0));  
  25.             if (tmp * tmp == data[i])  
  26.                 --cnt;  
  27.             printf("%d\n", cnt);  
  28.         }  
  29.         free(data);  
  30.     }  
  31.     return 0;  
  32. }  
我的,说实话真不知道错在哪里呀
#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!求的逻辑一样。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值