To begin or not to begin+Sum of Consecutive Prime Numbers

系统管理员 2023-07-07 05:40 99阅读 0赞

3.To begin or not to begin

  1. Problem Description
  2. A box contains black balls and a single red ball. Alice and Bob draw balls from this box without replacement, alternating after each draws until the red ball is drawn. The game is won by the player who happens to draw the single red ball. Bob is a gentleman and offers Alice the choice of whether she wants to start or not. Alice has a hunch that she might be better off if she starts; after all, she might succeed in the first draw. On the other hand, if her first draw yields a black ball, then Bobs chances to draw the red ball in his first draw are increased, because then one black ball is already removed from the box. How should Alice decide in order to maximize her probability of winning? Help Alice with decision.
  3. Input
  4. Multiple test cases (number of test cases50), process till end of input.
  5. For each case, a positive integer k (1k10^5) is given on a single line.
  6. Output
  7. For each case, output:
  8. 1, if the player who starts drawing has an advantage
  9. 2, if the player who starts drawing has a disadvantage
  10. 0, if Alice's and Bob's chances are equal, no matter who starts drawing
  11. on a single line.
  12. Sample Input
  13. 1 2
  14. Sample Output
  15. 0 1

此题关键还是要多尝试,手动计算一下

当k=1时,一个红球和一个黑球,先手获胜的概率P为

当k=2时,一个红球和两个黑球,先手获胜的概率P为

当k=3时,一个红球和三个黑球,先手获胜的概率P为

故,当k时,先手获胜的概率P为

  1. import java.util.Scanner;//个人感觉这道题毫无意义
  2. public class 决定是不是开始 {
  3. public static void main(String[] args) {
  4. Scanner sc=new Scanner(System.in);
  5. int n=0;
  6. for(int i=0;i<50;i++) {
  7. n=sc.nextInt();
  8. if (n%2==1) {
  9. System.out.println(0);
  10. }else {
  11. System.out.println(1);
  12. }
  13. }
  14. }
  15. }

4.Sum of Consecutive Prime Numbers

  1. 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
  2. numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
  3. Your mission is to write a program that reports the number of representations for the given positive integer.

Input

  1. 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

  1. 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

  1. 2
  2. 3
  3. 17
  4. 41
  5. 20
  6. 666
  7. 12
  8. 53
  9. 0

Sample Output

  1. 1
  2. 1
  3. 2
  4. 3
  5. 0
  6. 0
  7. 1
  8. 2

思路:

先找出范围内的素数存在p[i]中,在接受输入数据n,在p[i]中找出n在数组内的位置,返回来双层循环找和相等的素数。
下面这个java时间过不去

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc=new Scanner(System.in);
  5. int a,b,i,j,n,t,r,k=1;
  6. int[] p=new int[10001];
  7. int l=0;
  8. for(a=2;a<10000;a++) {
  9. for(b=a-1;b>1;b--) {
  10. if(a%b!=0)
  11. continue;
  12. else
  13. break;
  14. }
  15. if(b==1) {
  16. p[l]=a;
  17. l++;
  18. }
  19. }
  20. while(k>0) {
  21. n=sc.nextInt();
  22. if(n!=0) {
  23. int f=0;
  24. r=0;
  25. p[l]=10002;
  26. for(i=0;i<l;i++) {
  27. if(p[i]<=n&&p[i+1]>n) {
  28. f=i;
  29. break;
  30. }
  31. }
  32. for(i=f;i>=0;i--) {
  33. int sum=0;
  34. for(j=i;j>=0;j--) {
  35. sum+=p[j];
  36. if(sum==n) {
  37. r++;
  38. break;
  39. }
  40. if(sum>n) break;
  41. }
  42. }
  43. System.out.println(r);
  44. }
  45. }
  46. }
  47. }

C++解法

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cmath>
  5. using namespace std;
  6. bool vis[20000];
  7. int prime[3000];
  8. int top;
  9. void Init(){
  10. memset(vis,false,sizeof(vis));
  11. for(int i=2;i<=10000;i++){
  12. if(vis[i])
  13. continue;
  14. prime[top++]=i;
  15. for(int j=i*2;j<=10000;j+=i){
  16. vis[j]=true;
  17. }
  18. }
  19. }
  20. int main(){
  21. top=0;
  22. Init();
  23. int t,sum,k;
  24. while(1){
  25. scanf("%d",&t);
  26. if(t==0)
  27. break;
  28. k=0;
  29. for(int i=0;i<top;i++){
  30. sum=0;
  31. for(int j=i;j<top;j++){
  32. sum+=prime[j];
  33. if(sum==t){
  34. k++;
  35. break;
  36. }
  37. else if(sum>t){
  38. break;
  39. }
  40. }
  41. }
  42. cout<<k<<endl;
  43. }

这个能过,哈哈哈哈!!!!!,优化了一下打表

  1. import java.util.Scanner;
  2. public class Main{
  3. static boolean [] vis=new boolean[20000];
  4. static int [] prime=new int[3000];
  5. static int top;
  6. public static void fn() {
  7. for(int i=2;i<=10000;i++){
  8. if(vis[i])
  9. continue;
  10. prime[top++]=i;
  11. for(int j=i*2;j<=10000;j+=i){
  12. vis[j]=true;
  13. }
  14. }
  15. }
  16. public static void main(String[] args) {
  17. top=0;
  18. Scanner sc=new Scanner(System.in);
  19. fn();
  20. int t,sum,k;
  21. while(true){
  22. t=sc.nextInt();
  23. if(t==0)
  24. break;
  25. k=0;
  26. for(int i=0;i<top;i++){
  27. sum=0;
  28. for(int j=i;j<top;j++){
  29. sum+=prime[j];
  30. if(sum==t){
  31. k++;
  32. break;
  33. }
  34. else if(sum>t){
  35. break;
  36. }
  37. }
  38. }
  39. System.out.println(k);
  40. }
  41. }
  42. }

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读