17. Einstein primary series of math problems

Einstein's math

Problem Description

There is a long ladder, if each step across the second order, the last remaining first order, if each step across the third order, the last remaining 2-order, if each step across the fifth order, the last remaining 4 bands, if each step across 6 Order the last remaining step 5, only 7 cross each order, and finally just a step not left, I ask within 1 to N, the number of how many to meet?

problem analysis

With variable x represents the number of steps, then x should be met:

If the second-order cross each step, the last remaining stage 1 ==> x% 2 = 1

If third-order across each step, the last remaining stage 2 ==> x% 3 = 2

If the fifth-order cross each step, the last remaining step 4 ==> x% 5 = 4

If 6-tap across each step, the last remaining stage 5 ==> x% 6 = 5

If the step 7 cross each step, the last step is not just a left ==> x% 7 = 0

Thus, the number of steps should also meet all of the above conditions

algorithm design

This problem requires N input values, the number of solved memory to meet the requirements in the number of steps can be used in a range of 1-N while the cycle is repeated to allow a plurality of N read values, until the end of file is encountered before the end of the input character EOF

#include <stdio.h>

int main(void)
{
    long n = 600, sum, i;   //200, 400, 600,

    //while (scanf("ld", &n) != EOF)   //EOF ascii码为0x1A, window ->ctrl+z, linux->ctrl+d
        while (n)
        {
        printf("在1-%ld之间的阶梯数为:\n", n);
        sum = 0;
        for (i = 7; i <= n; i++) {
            /* !<阶梯数所满足的条件 */
            if (i % 7 == 0) {
                if (i % 6 == 5) {
                    if (i % 5 == 4) {
                        if (i % 3 == 2) {
                            sum++;      /* !<sum记录1-n之间的满足条件的阶梯个数*/
                            printf("%ld\n", i);
                        }
                    }
                }
            }
        }
        printf("在1-%ld之间,有%ld个数可以满足爱因斯坦对阶梯的要求,\n",n,sum);
        break;
    }
}

/* !<output */
    在1-600之间的阶梯数为:
    119
    329
    539
    在1-600之间,有3个数可以满足爱因斯坦对阶梯的要求,

    Process returned 0 (0x0)   execution time : 0.006 s
    Press any key to continue.

Guess you like

Origin www.cnblogs.com/xuzhaoping/p/11484634.html