Title Description Description
The rational number in any [0,1] p/q (p, q are natural numbers) must be decomposed into 1/r1+1/r2+1/r3+...+1/rk, and R1<r2<r3<...<rk. Of course such decomposition is not unique, such as 5/6=1/2+1/3=1/2+1/5+1/8+1/120, the second item in the first decomposition is larger than the second in the second decomposition, so we can define the first decomposition to be larger than the second decomposition type.
The program requires finding the maximum decomposition of the p/q.
input/output format input/output
Input Format:
Keyboard input P, q,1≤p≤q≤50
output Format:
From small to large in the output of each denominator in the decomposition, a line output a number
input and Output sample sample Input/output
sample Test point # #
Input Sample:
5 6
Sample output:
2 3
Ideas:
Assuming that 1/r is the true fraction of the largest molecule that can be decomposed from the p/q 1, then 1/r≤p/q<1/(r-1) ①
And because P/q-1/r= (PXR-Q)/(QXR) ②
According to ①, PX (r-1) <q, so pxr-p
After the input p, q to simplify the score.
The code is as follows:
1#include <stdio.h>2 intgcdintAintb) //Beg Greatest common divisor 3 {4 intr=a%b;5 while(r>0)6 {7A=b;8b=R;9r=a%b;Ten } One returnb; A } - intMain () - { the intp,q; - intcm//current Greatest common divisor - intR; -scanf"%d%d",&p,&q); + while(p>0) - { +cm=gcd (p,q); A if(cm>0) at { - /*===========*///Reduction of fractions -p=p/cm; -q=q/cm; - /*===========*/ - } in if((q%p) >0)//if not decomposed into the final 1/rk - { tor=q/p+1; + } - Else the { *r=q/p; $ }Panax Notoginsengprintf"%d\n", R); -P=p*r-q;//lose theq=q*R; + } A return 0; the}
Decomposition of rational numbers-number theory