分子为1的分数称为埃及分数。现输入一个真分数(分子比分母小的分数,叫做真分数),请将该分数分解为不同的埃及分数。如:8/11 = 1/2+1/5+1/55+1/110。
注:真分数指分子小于分母的分数,分子和分母有可能gcd不为1!
如有多个解,请输出任意一个。
输入一个真分数,String型
输出分解后的string
8/11 2/4
1/2+1/5+1/55+1/110 1/3+1/6
第二个样例直接输出1/2也是可以的
#include <stdio.h> /* 输入真分数为a/b 从大到小遍历埃及分数1/n 如果 a*n >= b 即 a/b >= 1/n 则 a/b = a/b-1/n 即 a= a*n-b b= b*n 然后对ab约分 如果约分结果a为1停止 注意:为了过最后一个例子,需要用8字节的long而不是4字节的int */ // a/b long a,b; void exec(){ if(a>1){ for(int i=2;i<=a;i++){ if(a%i==0&&b%i==0){ a/=i; b/=i; } } } } void decrease(long n){ long a1=a; long b1=b; if(a1*n>=b1){ a=a1*n-b1; b=b1*n; exec(); if(a==1){ printf("1/%ld+1/%ld",n,b); }else { printf("1/%ld+",n); } //return 1; } //return 0; } int main() { long n=2; scanf("%ld/%ld",&a,&b); while(a>1){ decrease(n); n++; } return 0; }
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <math.h> #define N 100 #define MIN 1e-6 bool isEqual(float x, float y) { if(fabs(x - y) < MIN) return true; return false; } int main() { int a, b; scanf("%d/%d", &a, &b); int res[N] = {0}; int cnt = 0, n = 2; float right = 0.0, left = 0.0; right = (float)a / b; bool flag = false; while(true){ int fenmu = n * b; int fenzi[N] = {0}; float temp = 0.0; int i = 2; cnt = 0; left = 0.0; while(true){ if(i > fenmu){ break; } if(fenmu % i == 0 && ((float)fenmu / i / fenmu) < right){ temp = (float)fenmu / i / fenmu; if(temp + left < right){ left += temp; fenzi[cnt++] = fenmu / i; }else if(isEqual(left+temp, right)){ fenzi[cnt++] = fenmu / i; flag = true; break; } } i++; } n++; if(flag){ for(int i = 0; i < cnt; i++){ res[i] = fenmu / fenzi[i]; } break; } } for(int i = 0; i < cnt-1; i++){ printf("1/%d+", res[i]); } printf("1/%d\n", res[cnt-1]); }
/*直接暴力破解,但是为什么有一个实例不通过*/ #include <stdio.h> #include <string.h> void print(int fenzi,int fenmu){ int input=fenzi; for(int i=2;;i++){ if(input*i>=fenmu){ input=input*i-(fenmu); fenmu=fenmu*i; printf("1/%d",i); if(input!=0){ printf("+"); } else{ break; } } } printf("\n"); return; } int main() { int a,b; while(scanf("%d/%d",&a,&b)!=EOF){ print(a,b); /*input=fenzi; for(double i=2.0;i<fenmu*fenmu;i=i+1.0){ if(input*i>(fenmu)){ input=input*i-(fenmu); fenmu=fenmu*i; printf("1/%d",(int)i); if(input!=0){ printf("+"); } } } printf("\n");*/ } return 0; }
#include "stdio.h" int main(void) { int a,b; while(scanf("%d/%d", &a, &b) != EOF) { for(int i = 0; i < a; i++) { printf("1/%d", b); if(i < a - 1) { printf("+"); } } printf("\n"); } return 0; } 直接搞成1/分母就过了 哈哈哈
#include <stdio.h> #include <stdint.h> struct fraction { uint64_t u; uint64_t d; }; uint64_t gcd(uint64_t a, uint64_t b) { while (1) { a%=b; if (a==0) { return b; } b%=a; if (b==0) { return a; } } } // a=a-b // return 0:if a<b // return 1:if a==b // return 2:if a>b && a-b is not埃及分数 // return 3:if a>b && a-b为埃及分数 uint64_t get_fit(struct fraction*const a, struct fraction b) { if ( a->d == b.d ) { if (a->u<b.u) { return 0; } if (a->u==b.u) { return 1; } a->u-=b.u; return 2; } //printf("11:%llu,%llu,%llu,%llu\n", a->u, a->d, b.u, b.d); struct fraction temp={a->u*b.d, a->d*b.d}; b.u*=a->d; b.d*=a->d; //printf("22:%llu,%llu,%llu,%llu\n", temp.u, temp.d, b.u, b.d); if (temp.u<b.u) { return 0; } if (temp.u==b.u) { return 1; } a->u=temp.u-b.u; a->d=temp.d; if (a->u ==1) { //printf("111111\n"); return 3; } const uint64_t gc=gcd(a->u, a->d); a->u/=gc; a->d/=gc; if (a->u==1) { //printf("222222\n"); return 3; } return 2; } int main() { struct fraction in; while (scanf("%llu/%llu", &in.u, &in.d) == 2) { struct fraction result[200]; uint64_t result_size=0; struct fraction temp={1, 2}; while (1) { uint64_t ret=get_fit(&in, temp); if (ret==0) { //printf("temp.d=%llu\n", temp.d); ++temp.d; continue; } else if (ret == 1) { result[result_size++]=temp; break; } else if (ret==3) { //printf("in=%llu\n", in.d); result[result_size++]=temp; result[result_size++]=in; break; } else { result[result_size++]=temp; ++temp.d; } } for (uint64_t i=0; ; ) { if (i == result_size-1) { printf("%llu/%llu", result[i].u, result[i].d); break; } else { printf("%llu/%llu+", result[i].u, result[i].d); ++i; } } putchar('\n'); } return 0; }