#include<vector>
#include<iostream>
std::vector<size_t> CalcStep(const std::vector<size_t>& sizes) {
if (sizes.empty())
return{};
std::vector<size_t> res(sizes.size());
res.back() = 1;
for (int i = res.size() - 2; i >= 0; --i)
res[i] = res[i + 1] * sizes[i + 1];
return res;
}
std::vector<size_t> CalcId(size_t id, const std::vector<size_t>& step) {
std::vector<size_t> res(step.size());
for (size_t i = 0; i < step.size(); ++i) {
res[i] = id / step[i];
id -= (step[i] * res[i]);
}
return res;
}
class FloatP {
public:
FloatP(double min, double max, double step) :
min_(min), max_(max), step_(step) {}
std::vector<double> GetRange() {
std::vector<double> values;
for (double v = min_; v <= max_; v += step_) {
values.emplace_back(v);
}
return values;
}
double min_, max_, step_;
};
void main() {
std::vector<std::vector<double>> floatRanges;
std::vector<size_t> floatRangeSizes;
int totalNb = 1;
FloatP p1(1, 5, 2), p2(11, 52, 2), p3(14, 35, 2);
std::vector<FloatP> ps{ p1,p2,p3 };
floatRanges.reserve(ps.size());
for (int id = 0; id < ps.size(); ++id) {
floatRanges.push_back(ps[id].GetRange());
floatRangeSizes.push_back(floatRanges.back().size());
totalNb *= floatRangeSizes.back();
}
std::cout << "floatRangeSizes = ";
for (auto&e : floatRangeSizes)
std::cout << e << ", ";
std::cout << "\n";
std::cout << "totalNb = "<< totalNb <<"\n";
std::vector<size_t> floatRangeStep = CalcStep(floatRangeSizes);
for (auto&e : floatRangeStep)
std::cout << e << ", ";
std::cout << "\n";
for (int id = 0; id < totalNb ; ++id) {
std::cout << "id = " << id << " idv = ";
std::vector<size_t> idv = CalcId(id, floatRangeStep);
for (auto&e : idv)
std::cout << e << ", ";
std::cout << "floatRanges = ";
for (int i = 0; i < floatRanges.size(); ++i)
std::cout << floatRanges[i][idv[i]] << ", ";
std::cout << "\n";
}
getchar();
}
c++ 多个浮动参数排列组合,从排列组合结果的索引倒推浮动参数是多少
于 2024-08-18 20:24:22 首次发布