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