"Han xin point soldier" problem solving method
There are a group of soldiers, exact number I don't know, but if every three people a group, is more than 2 people;
Each a set of 5 people, more than 3 people; Every seven people a group, more than five people; Every 11 people a group, more than 4 people,
The code
#include
using namespace std;
Int main () {
Int n=2;
While (n % 5!
=3){n=n + 15; }
While (n % 7!
=5){n=n + 105; }
While (n % 11!
=4){n=n + 1155; }
Cout & lt;
}
Can you tell me something wrong?
CodePudding user response:
While (n % 5!=3){n=n + 15; }//itself can be divided exactly by 5, 15 + 15 what's the point? If into the cycle is infinite loop
Change
Int I=5;//3 set of more than 2
For (; ; I +=3) {//are increasing 3 people each time, guarantee the group will be more than 2
If (I % 5==3 & amp; & I % 7==5 & amp; & I % 11==4) break;
}
Printf (" % d ", I);
CodePudding user response:
To such high efficiency someInt I=15;//more than 11 people group 4
For (; ; Every time I +=11) {//increment 11 people, guarantees the 11 people group will be more than 4
If (I % 5==3 & amp; & I % 7==5 & amp; & I % 3==2) break;
}
CodePudding user response:
I've just come from class, okay, thank you