1.3 SUM OF Consecutive Prime Numbers
2018-02-20 23:15
316 查看
离线计算 将连续素数存到数组中
POJ 2739
SUM OF Consecutive Prime Numbers
Description
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.InputThe 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.OutputThe 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 Input2
3
17
41
20
666
12
53
0Sample Output1
1
2
3
0
0
1
2package data20180220;
import java.util.ArrayList;
import java.util.List;
//POJ
import java.util.Scanner;
public class SumOfCPN {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int i=2;i<100001;i++){
if(isPrime(i)==true){
list.add(i);
}
}
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
while(num!=0){
int ans=0;
for(int i=0;num>=list.get(i);i++){
int sum=0;
for(int j= i;sum<num&&j<=list.size();j++){
sum+=list.get(j);
}
if(sum==num){
ans++;
}
}
System.out.println(ans);
num = sc.nextInt();
}
}
public static Boolean isPrime(int k){
for(int j=2;j<=Math.sqrt(k);j++){
if(k%j==0){
return false;
}
}
return true;
}
}
当日笔记:
数组 内存连续分配 索引速度快 利于赋值修改 不利于插入,移除。
指明长度 内存溢出或浪费。
ArrayList
动态扩充与收缩。
方便进行数据的添加、修改、插入与移除。
可存多种类型的数据。装箱 拆箱操作 麻烦。
List:ArrayList类的泛型等效类。
List不能被构造,但可以向上面那样为List创建一个引用,而ArrayList就可以被构造。
Listlist = new List(); //错误。
List<int>list = new List<int>(); //正确。
List<int>list = new ArrayList<>();//正确。
https://www.cnblogs.com/shouyeren/p/6201759.html
判断一个数是否为质数/素数:
即n是否能被2-根号n的数整除如果能,则为素数 不能,则不是素数。
POJ 2739
SUM OF Consecutive Prime Numbers
Description
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.InputThe 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.OutputThe 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 Input2
3
17
41
20
666
12
53
0Sample Output1
1
2
3
0
0
1
2package data20180220;
import java.util.ArrayList;
import java.util.List;
//POJ
import java.util.Scanner;
public class SumOfCPN {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int i=2;i<100001;i++){
if(isPrime(i)==true){
list.add(i);
}
}
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
while(num!=0){
int ans=0;
for(int i=0;num>=list.get(i);i++){
int sum=0;
for(int j= i;sum<num&&j<=list.size();j++){
sum+=list.get(j);
}
if(sum==num){
ans++;
}
}
System.out.println(ans);
num = sc.nextInt();
}
}
public static Boolean isPrime(int k){
for(int j=2;j<=Math.sqrt(k);j++){
if(k%j==0){
return false;
}
}
return true;
}
}
当日笔记:
数组 内存连续分配 索引速度快 利于赋值修改 不利于插入,移除。
指明长度 内存溢出或浪费。
ArrayList
动态扩充与收缩。
方便进行数据的添加、修改、插入与移除。
可存多种类型的数据。装箱 拆箱操作 麻烦。
List:ArrayList类的泛型等效类。
List不能被构造,但可以向上面那样为List创建一个引用,而ArrayList就可以被构造。
Listlist = new List(); //错误。
List<int>list = new List<int>(); //正确。
List<int>list = new ArrayList<>();//正确。
https://www.cnblogs.com/shouyeren/p/6201759.html
判断一个数是否为质数/素数:
即n是否能被2-根号n的数整除如果能,则为素数 不能,则不是素数。
相关文章推荐
- UVa1210 - Sum of Consecutive Prime Numbers(欧拉筛法即线性筛法)
- UVA 1210 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers(素数表)
- POJ 2739 Sum of Consecutive Prime Numbers(水题)
- POJ.2739 Sum of Consecutive Prime Numbers(水)
- poj 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers(尺取法)
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ2739-Sum of Consecutive Prime Numbers【尺取法】
- UVa 1210 - Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers - UVa 1210 水题
- POJ 2739 Sum of Consecutive Prime Numbers(素数表的应用)
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ-2739 Sum of Consecutive Prime Numbers(尺取法+埃拉托斯特尼筛法)
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers 解题报告
- poj 2739 Sum of Consecutive Prime Numbers