C Exercise Example 80

100 Classic C Examples

Title:There is a pile of peaches on the beach, and five monkeys come to divide them. The first monkey divides the pile equally into five portions, with one extra. This monkey throws the extra peach into the sea and takes away one portion. The second monkey divides the remaining peaches equally into five portions again, with one extra. It likewise throws the extra peach into the sea and takes away one portion. The third, fourth, and fifth monkeys all do the same. What is the minimum number of peaches that were originally on the beach?

Program analysis:None.

Program source code:

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. All rights reserved. // #include <stdio.h> int main() { int n = 0; // n represents the initial number of peaches int i, count; int found = 0; while (!found) { n += 1; // Assume the initial number of peaches count = n; for (i = 0; i < 5; i++) { if ((count - 1) % 5 == 0) { count = (count - 1) / 5 * 4; } else { break; } } if (i == 5) { found = 1; } } printf("There are at least %d peaches.\n", n); return 0; }

The output result of running the above example is:

3121

100 Classic C Examples

other extensions