#include <algorithm> #include <iostream> #include <map> #include <stdio.h> #include <stdlib.h> #include <vector> double CalcEquivalentR(const std::vector<double> &R, unsigned sw) { double ER = 0; for (unsigned i = 0; i < R.size(); ++i) { if ((sw >> i) & 0x01) { if (R[i] == 0.0) { return 0.0; } else { ER += 1.0 / R[i]; } } } return 1.0/ER; } void ShowEquivalentR(const std::vector<double> &R) { std::map<double/*等效電阻*/, unsigned/*組合方式*/> ER; unsigned n = 1 << R.size(); for (unsigned sw = 1; sw < n; ++sw) { double er = CalcEquivalentR(R, sw); ER[er] = sw; } for (std::map<double, unsigned>::iterator it = ER.begin(); it != ER.end(); ++it) { std::cout << "when turn on("; unsigned sw = it->second; for (unsigned i = 0; i < R.size(); ++i) { if ((sw >> i) & 0x01) { std::cout << R[i] << ","; } } std::cout << ") the equal R is " << it->first << std::endl; } } int main() { std::vector<double> R = {3.2,3.7,4.7,5.0}; // 修改 R ShowEquivalentR(R); }
Standard input is empty
when turn on(3.2,3.7,4.7,5,) the equal R is 1.00448 when turn on(3.2,3.7,4.7,) the equal R is 1.25701 when turn on(3.2,3.7,5,) the equal R is 1.27751 when turn on(3.2,4.7,5,) the equal R is 1.3788 when turn on(3.7,4.7,5,) the equal R is 1.46405 when turn on(3.2,3.7,) the equal R is 1.71594 when turn on(3.2,4.7,) the equal R is 1.9038 when turn on(3.2,5,) the equal R is 1.95122 when turn on(3.7,4.7,) the equal R is 2.07024 when turn on(3.7,5,) the equal R is 2.12644 when turn on(4.7,5,) the equal R is 2.42268 when turn on(3.2,) the equal R is 3.2 when turn on(3.7,) the equal R is 3.7 when turn on(4.7,) the equal R is 4.7 when turn on(5,) the equal R is 5