30 difficult problems: counter problems and prime arithmetic problems

Holidays make people become pigs. In fact, there have been several impulses to write blogs in more than a month, but each time they write half, they don't stick to it, and school starts in a twinkling of an eye.
During the holiday, I saw two interesting topics: the problem of counters and the problem of prime arithmetic sequence.

1. Counter problem

Inadvertently, I saw such a problem and suddenly became interested.

#include<iostream>
using namespace std;
int main(){
 int count;
 count=0;
 int x;
 x=0;
 while(x<=8){
  int ra=rand()%2;
  if(ra=0){
  x++;
  }
  else{
   x--;
  }
  count++;
 }
 cout<<count<<endl;
 return 0;
}

The output result is negative, indicating that the number of operations is large, and a problem is exposed at the same time. It is a huge amount of calculation to calculate the average number of events.

Replacing int with double can finally give me a number:

#include<iostream>
using namespace std;
int main(){
 double count;
 count=0;
 int x;
 x=0;
 while(x<=8){
  int ra=rand()%2;
  if(ra=0){
  x++;
  }
  else{
   x--;
  }
  count++;
 }
 cout<<count<<endl;
 return 0;
}

But the unsolved problem is, how to get an average value? This may have to be solved in the way of probability theory. At the same time, a very simple mathematical problem also reveals an imperfection in c + + that I think is the rand function.

At the end of last month, the card game "undetermined event book" was born. I scratched my hand and wrote a simulator to simulate card drawing, but I didn't expect to hit a wall here in random number generation. The simulator written by my friend vb became, so I gave up to overcome this problem.

It's sad that a quick forming applet was abandoned. I tried to use seeds and srand (unsigned) time (null), but if the button clicks fast, I still can't generate the desired random number.

2. Prime arithmetic sequence problem

This is a question I saw when watching the real question of the Blue Bridge Cup. At first glance, I may feel that it is not difficult, but if this question is regarded as an equation problem, there is actually an unknown number, but I want to write it in a main function and give up the idea of solving it violently in the final triple cycle.
This is the failure code I wrote. I'm going to find the problem and then go back to find my own problem:

#include <iostream>
using namespace std;
int p1[100010];
int main()
{
 for(int m=0;m<20000;m++){
  p1[m]=1;
 }
 for(int i=4;i<20000;i++){
  for(int j=2;j<i;j++){
   if(i%j==0){
    p1[i]=0;
    break;
   }
  }
 }
 for(int n=3;n<20000;n++){
  if(p1[n]){
   for(int s1=30;s1<1000;s1++){
   for(int s2=1;s2<=10;s2++){
    if(p1[n+s1*s2]=0){
     break;
    }
   }
   if(s2=10){
    cout<<s1<<endl;
    break;
   }
   }
  }
 }
 return 0;
}


The output is in a mess...

Answer reference

#include <bits/stdc++.h>
using namespace std;
int p[100010];
int prim[100010];
int len=0;
void isp()
{
    memset(p,0,sizeof(p));
    p[0]=1;p[1]=1;p[2]=0;
    for(int i=0;i<10000;i++)
    {
        if(p[i])
            continue;
        for(int j=i;j*i<10000;j++)
        {
            p[i*j]=1;
        }
        prim[len++]=i;
    }
}
int main()
{
    isp();
    for(int i=0;i<len;i++)
    {
         int ss=prim[i];
         for(int c=1;c<1000;c++)
         {
             int j;
             for(j=1;j<10;j++)
             {
                 if(p[ss+c*j])
                    break;
             }
             if(j>=10)
             {
                 cout<<c<<' '<<ss<<endl;
                 return 0;
             }
         }
    }
}

Posted by naggi on Thu, 19 May 2022 12:47:44 +0300