Sum of Consecutive Prime Numbers
4.Sum of Consecutive Prime Numbers
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.
Input
The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.
Output
The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.
Sample Input
2
3
17
41
20
666
12
53
0
Sample Output
1
1
2
3
0
0
1
2
思路:
先找出范围内的素数存在p[i]中,在接受输入数据n,在p[i]中找出n在数组内的位置,返回来双层循环找和相等的素数。
下面这个java时间过不去
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a,b,i,j,n,t,r,k=1;
int[] p=new int[10001];
int l=0;
for(a=2;a<10000;a++) {
for(b=a-1;b>1;b--) {
if(a%b!=0)
continue;
else
break;
}
if(b==1) {
p[l]=a;
l++;
}
}
while(k>0) {
n=sc.nextInt();
if(n!=0) {
int f=0;
r=0;
p[l]=10002;
for(i=0;i<l;i++) {
if(p[i]<=n&&p[i+1]>n) {
f=i;
break;
}
}
for(i=f;i>=0;i--) {
int sum=0;
for(j=i;j>=0;j--) {
sum+=p[j];
if(sum==n) {
r++;
break;
}
if(sum>n) break;
}
}
System.out.println(r);
}
}
}
}
C++解法
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
bool vis[20000];
int prime[3000];
int top;
void Init(){
memset(vis,false,sizeof(vis));
for(int i=2;i<=10000;i++){
if(vis[i])
continue;
prime[top++]=i;
for(int j=i*2;j<=10000;j+=i){
vis[j]=true;
}
}
}
int main(){
top=0;
Init();
int t,sum,k;
while(1){
scanf("%d",&t);
if(t==0)
break;
k=0;
for(int i=0;i<top;i++){
sum=0;
for(int j=i;j<top;j++){
sum+=prime[j];
if(sum==t){
k++;
break;
}
else if(sum>t){
break;
}
}
}
cout<<k<<endl;
}
这个能过,哈哈哈哈!!!!!,优化了一下打表
import java.util.Scanner;
public class Main{
static boolean [] vis=new boolean[20000];
static int [] prime=new int[3000];
static int top;
public static void fn() {
for(int i=2;i<=10000;i++){
if(vis[i])
continue;
prime[top++]=i;
for(int j=i*2;j<=10000;j+=i){
vis[j]=true;
}
}
}
public static void main(String[] args) {
top=0;
Scanner sc=new Scanner(System.in);
fn();
int t,sum,k;
while(true){
t=sc.nextInt();
if(t==0)
break;
k=0;
for(int i=0;i<top;i++){
sum=0;
for(int j=i;j<top;j++){
sum+=prime[j];
if(sum==t){
k++;
break;
}
else if(sum>t){
break;
}
}
}
System.out.println(k);
}
}
}
还没有评论,来说两句吧...