When Einstein stepped up the stairs, if every step was 2 steps, there were 1-order two ways.

Source: Internet
Author: User

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;
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.