Files
Data-Structure/Algorithm/IterativeDeepening/Egypt-Fraction-BAD.cpp
e2hang 265b8af720 IDS
2025-09-11 15:53:33 +08:00

50 lines
1.1 KiB
C++

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// f = a/b
double checkv(const vector<int>& x){
double c = 0.0;
for(auto q : x){
c += 1.0 / q;
}
return c;
//return fabs(c - r) < 1e-9; // 浮点数近似比较
}
//以step作为迭代标志
bool dfs(vector<int>& path, int step, int max_depth, int a, int b, int max_val){
double r = static_cast<double>(a) / b;
if(fabs(checkv(path) - r) < (1e-9)){
for(int x : path) cout << x << " ";
cout << endl;
return true;
}
if(checkv(path) > r + (1e-9)) return false;
if(step == max_depth) return false;
int start = path.empty() ? 1 : path.back();
for(int i = start; i <= max_val; i++){
path.push_back(i);
if(dfs(path, step + 1, max_depth, a, b, max_val)) return true;
path.pop_back();
}
return false;
}
bool ids(int limit, int a, int b, int max_val){
for(int depth = 1; depth <= limit; depth++){
vector<int> path;
if(dfs(path, 0, depth, a, b, max_val)) return true;
}
return false;
}
int main(){
int aa, bb, max;
cin >> aa >> bb;
max = 200;
ids(10, aa, bb, max);
return 0;
}