C Programming Language 100 cases of (3): Cantor table

Example 3 Cantor table

Title Description

One of the famous proof of modern mathematics is Georg Cantor proved the rational numbers is enumerable. He is following this one table to prove this proposition:

1/1  1/2  1/3  1/4  ……

2/1  2/2  2/3  ……

3/1  3/2  ……

4/1  ……

……

The method is to give the z-shaped table for each number. Method: The first term is 1/1, then 1 / 2,2 / 1,3 / 1,2 / 2,1 / 3,1 / 4,2 / 3 .......

Input Format

Integer N (1≤N≤10000000)

Output Format

Item table N

SAMPLE INPUT

7

Sample Output

1/4

         (1) 1 programming ideas.

        The number of tables may be above 45 degrees right turn, into a pyramid shape, as shown below:

1/1

2/1  1/2   

3/1  2/2  1/3

4/1  3/2  2/3  1/4

…… …… …… ……

        The pyramid has a number of first row, the second row has the number 2, ..., i have a number of i-th row. And the numerator of the fraction on the i-th row i from the i ~ 1, the denominator from 1 ~ i, i.e. a first fraction i / 1, a final score of 1 / i.

        The section of the N number of outputs of the table, to be calculated first few lines of this one. Item disposed in N rows x, x-1 since the front row have item 1 + 2 + 3 + ... + (x-1), with a front row x 1 + 2 + ... + x term, therefore:

                    

        A loop can count the number of lines where n items, as follows:

                  for (i=1; i*(i+1)/2<n; i++) ;

        After the loop exits, i * (i + 1) / 2 is just greater than or equal to n, therefore, i is the row where the first n items.

        N belonging to the first item and several can be calculated in the i-th row, the formula is:

                   Subtracting the number of all items i.e. n i-1 of the front row.

        Z is due to the method of each font number table number, so that when the line number is odd, be numbered from left to right; when the line number is an even number, numbered from right to left for.

        Thus, when i is an odd number, k-th i-th row is (i + 1-k) / (k); when i is an even number, k-th i-th row is (k) / (i + 1 -k).

        (2) a source.

#include <stdio.h>

int main ()

{

    int i,k,n;

    scanf("%d",&n);

    for (i=1; i*(i+1)/2<n; i++) ;

    k=n-(i*(i-1)/2);

    if (i%2!=0)

       printf("%d/%d\n",i+1-k,k);

    else

       printf("%d/%d\n",k,i+1-k);

    return 0;

}

        (3) 2 programming ideas.

        Direct simulation determined the number should be in the N-th row of the column through line s cyclic manner.

        (4) 2 source.

#include <stdio.h>

int main ()

{

    int n,s,row;

    scanf("%d",&n);

         row=1;

         s=1;

         while (s<n)

         {

                   row++;

                   s+=row;

         }

         s=n-(s-row);

         if (row%2==1)

                   printf("%d/%d\n",(row-s)+1,s);

         else

                   printf("%d/%d\n",s,(row-s)+1);

         return 0;

}

Problem 3

3-1 gold

        This question is selected from the Los Valley exam (https://www.luogu.org/problem/P2669)

Title Description

The king gold as wages paid to loyal knight. The first day, Knight received a gold coin; two days later (second and third day), received two gold coins every day; three days later (fourth, five or six days), received three gold coins every day ; four days later (seventh, eight, nine, ten), received four gold ...... every day; this mode of payment of wages will always continue like this: when N N consecutive days to receive gold coins every day, Knights consecutive days following the N + 1, the received N + 1 coins per day.

Calculate K previous days, the Knights won a total of how many gold coins.

Input Format

A positive integer K, the number of days of issuance of coins.

Output Format

A positive integer that is the number of coins Knight received.

Input Sample # 1

6

Sample Output # 1

14

Input Sample # 2

1000

Output Sample # 2

29820

Description / Tips

Sample Description 1 [O]

Knight first day of the receipt of a gold coin; second and third day, received two gold coins every day; fourth, five or six days, received three gold coins every day. Therefore it received a total of 1 + 2 + 3 + 2 + 3 + 3 = 14 coins.

            (1) 1 programming ideas.

        N days for the previous calculation, the Knights won a total of how many gold coins. N is determined on fired several days of gold. Provided the N-th days of x coins, since hair x-1 coins can be made 1 + 2 + 3 + ... + (x-1) day, hair x coins can be made 1 + 2 + ... + x days, therefore :   

                 

        A loop can calculate the number of days of coins of n, as follows:

               for (i=1; i*(i+1)/2<n; i++) ;

        After the loop exits, i * (i + 1) / 2 is just greater than or equal to n, therefore, i is the number of days of the n coins. 1 ~ i-1 coins must send full appropriate number of days, i.e. one coins made by one day per day, day two coins made 2 days, ..., day i-1 coins issued i-1 day before cumulative circulating day made 1 ~ i-1 the number of coins were made, plus the number of issued coins delivered to the i-th day answer.

          (2) a source.

#include <stdio.h>

int main ()

{

         int n,m,k,s,i;

         scanf("%d",&k);

    for (n=1; n*(n+1)/2<k; n++);

    n--;

         m=(1+n)*n/2;

         for (s=0,i=1;i<=n;i++)

                   s+=i*i;

         if (k-m>0)

                   s+=(k-m)*(n+1);

         printf("%d\n",s);

         return 0;

}

           (3) 2 programming ideas.

            Direct simulation of hair gold process. Done with a double loop, the number of days of the outer-loop control, the inner loop control loop with k k coins bursts days.

            (4) 2 source.

#include <stdio.h>

int main ()

{

    int n,k=1,i=1,j,sum=0; 

    scanf("%d",&n);  

    while (i <= n) // has loop until

    { 

       for (j = 1; j <= k; j ++) // even days to k 

       { 

          sum = sum + k; // add money available today   

          i ++; // increase in the total number of days  

          if(i>n) 

          { 

             break; 

          } 

       } 

       k ++; // even to increase the number of days 

    } 

         printf("%d\n",sum);

         return 0;

}

 

3-2 brine story

Problem Description

When linked to salt water, if it drops regularly, first drop, pause; then drop two drops, pause; then drop three drops, pause ... and now there is a problem: this bottle of saline, a total VUL ml, every drop is D ml, every drop of speed is one second (assuming that the last drop of less than D ml, the time it takes to be considered a second), pause time is one second bottle of water when it can be linked to finish it?

Input

Input data comprising a plurality of test cases, one row for each instance, the VUL, and D, where 0 <D <VUL <5000.

Output

For each set of test data, output after hanging saline time required, the output of each row for instance.

Sample Input

10 1

Sample Output

13

      (1) programming ideas.

        First calculate how much vul ml saline DDD play. Obviously, the number of drop N = (int) (vul / d);, if not the last drop d ml, and takes the counted one second, thus to be noted that the number of drops required to achieve accuracy, can be corrected as follows:

if (vul-N*d>0.00001)  N++;

        Is the time required after hanging saline calculations necessary to determine the number of intermediate pauses. Provided dropwise Play N drops of saline needed dropwise x times, since the front x-1 times may be dropped 1 + 2 + 3 + ... + (x-1) drop of brine, before x times may be dropped 1 + 2 + ... + x dropwise brine, therefore:

                

        Play N times may be calculated dropwise with a desired droplet brine cycle, as follows:

             for (i=1; i*(i+1)/2<n; i++) ;

        After the loop exits, i * (i + 1) / 2 is just greater than or equal to n, therefore, i is the number of droplets N required to play the brine droplets. It will pause between every two, thus the dwell time of i-1 seconds, when coupled with N, N seconds dropwise to obtain the required answers.

         (2) source.

#include <stdio.h>

int main ()

{

         double vul,d;

         int s,n;

    while (scanf("%lf%lf",&vul,&d)!=EOF)

    {

        s = (int) (VUL / d);

        if (vul-s*d>0.00001) s++;

        for (n=1; n*(n+1)/2<s; n++);

        n--;

        printf("%d\n",s+n);

    }

         return 0;

}

3-3 Peter smoke

Title Description

Peter has n cigarette, each finished smoking a cigarette he put the cigarette butts saved, k (k> 1) cigarette butts can change a new cigarette, then how much Peter eventually be able to breathe cigarette it?

Input Format

Test data comprises a plurality of sets, each set of test data of one line comprises two integers n-, K (. 1 <n-, k≤10 . 8 ).

Output Format

For each test, including the output line represents an integer the number of the final smoke.

SAMPLE INPUT

4 3

10 3

100 5

Sample Output

5

14

124

         (1) programming ideas.

         Provided the total number of the variable sum representative of the smokable Peter, n represents the number of cigarette, because there are n Peter cigarette, so initially, n = sum (n smokable cigarette), butts number n (n cigarette generating n cigarette butts).

After the continuous exchange with cigarette butts, cigarette butts change since k a cigarette, thus continuously performed

            sum = sum + n / k; (n cigarette butts pumping transducer n / k cigarette)

             n = n / k + n% k; (n / k to produce cigarette n / k cigarette butts, cigarette plus the change in front of the transducer is less than the excess n% ​​k 1 th cigarette butts butts new number)

        This process is repeated until the remaining number of cigarette butts is not sufficient to change.

       (2) source.

#include <stdio.h>

int main ()

{

    int n,k,sum;

    while(scanf("%d%d",&n,&k)!=EOF)

    {

        sum = n;

        while(n >= k)

        {

            sum += n/k;

            n = n/k + n%k;

                   }

        printf("%d\n",sum);

    }

         return 0;

}

Guess you like

Origin www.cnblogs.com/cs-whut/p/11854561.html