The realization of C language monkey eating peach problem algorithm solution _c language

Source: Internet
Author: User

This article is an example of the realization of C language monkey eating peach problem. Share to everyone for your reference, specific as follows:

Problem:

The first day the monkey picked n a peach, then ate half, still not enjoyable, and then ate one. The next day he ate half the remaining peaches and ate one more. Every day after eating the last half of the remaining one. On the 10th day when you want to eat, there is only one peach left, ask the first day a total number of peaches off?

Analytical:

① from the last day of the x=1, pour out the number of X days before, you need to note that the expression is x=2 (x+1), rather than x=2x+1, pay attention to the difference between the two, think clearly why the second is not correct.

② the expression as the loop body that loops 9 times and sets a breakpoint at the statement to observe.

Specific procedures:

#include <stdio.h>
int main ()
{
  int i;
  int x=1;
  for (i=0;i<9;i++)
  x = 2* (x+1);
  printf ("%d\n", x);
  return 0;
}

The result of program operation is: 1534.

I hope this article will help you with the C language program design.

Related Article

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.