Einstein came up with such a mathematical problem: there is a long ladder, if each step across 2, then the last one, if each step across 3, then the last 2 order, if each step across the 5-order, then the last 4 order, if each step across the 6-order, the last remaining 5 order. Only 7 steps at a time, the end is just the first order of no left. How many steps are there in this ladder?
Two ways:
Method 1: Experiment from 1 onwards
unsigned int getstairnum ()
{for
(unsigned int n = 1;; ++n)
{
if (1 = n% 2) && (2 = = n% 3) && (4 = n% 5) && (5 = n% 6) && (0 = n% 7)) return
n;
}
Or:
unsigned int getstairnum ()
{
unsigned int stairnum = 0;
int flag=1;
while (flag)
{
if (stairnum%2 ==1 && stairnum%3 ==2 && stairnum%5 = 4 && stairnum%6 = 5 && stairnum%7 = = 0)
{
flag =0;
return stairnum;
}
stairnum++;
}
return 0;
}
Method Two: Stair order is an odd number, and is a multiple of 7, so you can cross 14 per step (7+14 is odd, 7+7 is even), a loop
unsigned int getstairnum ()
{
int n = 7;
while (1)
{
if ((1 = n% 2) && (2 = n% 3) && (4 = n% 5) && (5 = n% 6)) return
n;< C7/>else
n + +
}
}
Or:
unsigned int getstairnum ()
{
unsigned int n = 7;
while ((1!= n% 2) | | (2!= n% 3) | | (4!= n% 5) | | (5!= n% 6))
n + +;
return n;
}