Solving the problem of fish splitting by recursion

Source: Internet
Author: User

Problem:

5 people at night fishing, morning a first woke up, the fish divided into 5 parts, will be more one thrown into the sea, and then B woke up, do not know that a has taken a fish, the remaining fish into 5 parts, throw away the extra one, and then c,d,e woke up, according to the same method to separate fish, asked how many fish in partnership, How many fish do everyone see when they wake up?

#include <stdio.h> #include <stdlib.h>int fish (int n, int x);/* Run this program using the console Pauser or ad D your own getch, System ("pause") or input loop */int main (int argc, char *argv[]) {int i=0, flag = 0, x;do{i=i+1;x=i*5+1; if (fish (5,x))//recursive judgment {flag = 1;     Flag logo printf ("Total number of fish caught by the five-person partnership is%d\n", x);}} while (!flag); return 0;} int fish (int n, int x)//x represents the number of people, and X means that the remaining fish {if (x%5==1) {if (n==1) return 1;elsereturn fish (n-1, (x-1)/5*4) after waking up;} return 0;} Here the function of recursion is as judging condition


Solving the problem of fish splitting by recursion

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.