To: there are 100 meters long runway m people, some facing left, some facing right, each
People can only walk along the runway, the speed is 1 m/SEC, when two people meet, they would at the same time,
Turned around and went away in the opposite direction, these people, there are 1 people infected with the new type of coronavirus,
When and meet other people, the coronavirus infection to encounter people, please calculate,
When everyone left the runway, how many people are infected with the new type of coronavirus,
CodePudding user response:
This problem is easy to write a program to measure the, but also related to everyone's initial position and heading,The test code is to set the position and facing with random, so every time the results are different, also said that the conditions of the subject is not enough, how many people are infected with not sure value
# define MAX_LEN 100
The class Runner {
Private:
Int ill=0;
Int dir=1;
Int pos=0;
Public:
Runner (int pos, int dir) {this - & gt; Pos=pos; This - & gt; Dir=dir; }
~ Runner () {}
Void changeDir () {dir *=1; }
Void setIll () {ill=1; }
Void the run () {pos +=dir; }
Int getDir () {return dir. }
Int getPos () {return pos. }
Int isIll () {return ill; }
Int isOut () {return (pos<=0 | | pos>=MAX_LEN)? 1:0; }
};
Int main (int arg c, const char * argv []) {
Int m=5;
//the scanf (" % d ", & amp; M);
Srand ((unsigned int) time (NULL));//get a random number
Runner Runner=new Runner * * * [m].
Int dirs [2]={1, 1};
Int ill=rand () % m; A random sick//
for (int i=0; iInt pos=rand () % MAX_LEN;//random position
Int dir=dirs [rand () % 2];//random direction
Runner [I]=new runner (pos, dir);
If (I==) runner [I] - & gt; SetIll ();
}
Int out=0;
While (out & lt; M) {//and don't leave the runway has been circulating
for (int i=0; iIf (runner [I] - & gt; IsOut ()) {
Out++;
continue;
}
For (int j=0; jIf (I==j | | runner [j] - & gt; Continue isOut ());
If (runner [j] - & gt; GetPos ()==runner [I] - & gt; GetPos ()) {//if they have the same position,
If (runner [j] - & gt; IsIll () | | runner [I] - & gt; IsIll ()) {//and one infection
Runner [j] - & gt; SetIll ();//are both infection
Runner [I] - & gt; SetIll ();
}
If (runner [j] - & gt; GetDir ()!=runner [I] - & gt; GetDir ()) {//if the two sides in a different direction,
Runner [j] - & gt; ChangeDir ();//the change direction each other
Runner [I] - & gt; ChangeDir ();
}
}
}
}
for (int i=0; iif (! Runner [I] - & gt; IsOut ())
Runner [I] - & gt; The run ();//if you don't have to leave the runway will continue to run
}
Ill=0;
for (int i=0; iIll +=runner [I] - & gt; IsIll ();//statistical infections
Printf (" all ills is % d \ n ",);
for (int i=0; iThe delete runner [I];
The delete [] runner;
return 0;
}