Sum of Consecutive Prime Numbers III
来源:互联网 发布:ubuntu home 改成英文 编辑:程序博客网 时间:2024/06/07 16:34
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.
输入
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.
输出
The output should be composed of lines each corresponding to an input line except the last zero.
严格按样例的格式输出。
与每个输入数据相对应,首先输出"Integer XXX"(XXX代表输入的整数),然后看是否有解,如果有解,按样例格式逐行输出各个解(多行解的输出顺序是,加式中开头数字越小的排得越前);如果无解,输出"No Answer"
样例输入
5341340
样例输出
Integer 53----Answer 1: 5+7+11+13+17----Answer 2: 53Integer 41----Answer 1: 2+3+5+7+11+13----Answer 2: 11+13+17----Answer 3: 41Integer 3----Answer 1: 3Integer 4----No Answer
#include<stdio.h>#include<math.h>int main(){int i, j, k,z, a, b, c,d,n, s, sum[5010],e[5010], step;while (scanf("%d", &a) != EOF){if (a == 0) break;i = 0; j = 0;for (n = 2; n <= a; n++){for (s = 2; s*s <= n; s++) //rt的算法if (n%s == 0)break;if (s*s>n){sum[i] = n;// 记录所有素数i++;j = i;}}b = 0; step = 0; k = 0; d = 0;while (j){i = k;for (; sum[i] != '\0'; i++){step = step + sum[i];if (step == a){e[d] = k;e[d + 1] = i;// sum[k]...sum[i]b++;d+=2;k++; step = 0;break;}if (step>a){k++;step = 0;break;}}j--;}if (b == 0){printf("Integer %d\n",a);printf("----No Answer\n");}if (b>0){printf("Integer %d\n", a); //有答案n = 1 ;for (i = 0,z=0; z<b;z++, i+=2) //每一次循环代表以此数据{j = e[i]; // sum[j]k = e[i + 1]; // sum[k]printf("----Answer %d: ", n);if (j<k){for (; j <=k; j++)//大于一个数的一组数据{printf("%d", sum[j]);if (j < k) printf("+");}printf("\n");}if (j == k)//{printf("%d",a);printf("\n");}n++;}}//printf("%d\n", b); 组数}return 0;}
- Sum of Consecutive Prime Numbers III
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers pku 2739
- POJ2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- 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
- 国嵌qcd小功能在Ubuntu12.04上的实现
- POJ 3264 Balanced Lineup
- 一路Hadoop --1、Hadoop环境与使用的工具
- strcpy
- C语言经典算法100例-027-用递归逆序输出字符
- Sum of Consecutive Prime Numbers III
- Android 自定义title
- JDciom安装出现“unable to install JRE 1.3 on your system”
- 好好利用Github管理自己的代码
- UVA - 10405 Longest Common Subsequence(动态规划:LCS)
- leetcode Add Two Numbers
- 教练是协助者
- 黑马程序员-JAVA if语句练习
- linux间通讯