5 kyu some egyptian fractions

Given a rational number n

n >= 0, with denominator strictly positive

as a string (example: “2/3” in Ruby, Python, Clojure, JS, CS, Go)
or as two strings (example: “2” “3” in Haskell, Java, CSharp, C++, Swift, Dart)
or as a rational or decimal number (example: 3/4, 0.67 in R)
or two integers (Fortran)
decompose this number as a sum of rationals with numerators equal to one and without repetitions (2/3 = 1/2 + 1/6 is correct but not 2/3 = 1/3 + 1/3, 1/3 is repeated).

The algorithm must be “greedy”, so at each stage the new rational obtained in the decomposition must have a denominator as small as possible. In this manner the sum of a few fractions in the decomposition gives a rather good approximation of the rational to decompose.

2/3 = 1/3 + 1/3 doesn’t fit because of the repetition but also because the first 1/3 has a denominator bigger than the one in 1/2 in the decomposition 2/3 = 1/2 + 1/6.

Example:
(You can see other examples in “Sample Tests:”)

decompose(“21/23”) or “21” “23” or 21/23 should return

[“1/2”, “1/3”, “1/13”, “1/359”, “1/644046”] in Ruby, Python, Clojure, JS, CS, Haskell, Go

“[1/2, 1/3, 1/13, 1/359, 1/644046]” in Java, CSharp, C++, Dart

“1/2,1/3,1/13,1/359,1/644046” in C, Swift, R
Notes
The decomposition of 21/23 as

21/23 = 1/2 + 1/3 + 1/13 + 1/598 + 1/897
is exact but don’t fulfill our requirement because 598 is bigger than 359. Same for

21/23 = 1/2 + 1/3 + 1/23 + 1/46 + 1/69 (23 is bigger than 13)
or
21/23 = 1/2 + 1/3 + 1/15 + 1/110 + 1/253 (15 is bigger than 13).
The rational given to decompose could be greater than one or equal to one, in which case the first “fraction” will be an integer (with an implicit denominator of 1).

If the numerator parses to zero the function “decompose” returns [] (or “”, or “[]”).

The number could also be a decimal which can be expressed as a rational.

Example:
0.6 in Ruby, Python, Clojure,JS, CS, Julia, Go

“66” “100” in Haskell, Java, CSharp, C++, C, Swift, Scala, Kotlin, Dart, …

0.67 in R.

Ref: http://en.wikipedia.org/wiki/Egyptian_fraction

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char* dtoa( long long a , char* str )
{
long long temp = a, len = 0, count = 1;

while( temp > 0 )
{
len++;
temp /= 10;
count *= 10;
}

for( int i = 0; i < len; i++ )
{
str[i] = (char)( a / (int)pow( 10 , len - 1 - i ) + '0' );
count /= 10;
a %= count;
}
str[len] = '\0';

return str;
}
  char* decompose(char* nrStr, char* drStr) 
{
char* str = (char *)calloc( 256, sizeof(char) );
long long n = atol( nrStr ), d = atol( drStr ), MinD;

if( n / d >= 1 )//大于一的部分
{
char tmp[20] = {0};
MinD = n / d;

dtoa( MinD, tmp );
strcat( str, tmp );
if( n % d != 0 )
  strcat( str, "," );

n -= MinD * d;
}

while( d != 0 && n != 0 )
{
char tmp[20] = {0};
strcat( str, "1/" );

if( d % n )
  MinD = d / n + 1;
else
  MinD = d / n;

strcat( str, dtoa( MinD, tmp ) );

n = n * MinD - d;
d *= MinD;

if( d != 0 && n != 0 )
  strcat( str, "," );
}

return str;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值