fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <vector>
  7.  
  8. double CalcEquivalentR(const std::vector<double> &R, unsigned sw)
  9. {
  10. double ER = 0;
  11. for (unsigned i = 0; i < R.size(); ++i)
  12. {
  13. if ((sw >> i) & 0x01)
  14. {
  15. if (R[i] == 0.0)
  16. {
  17. return 0.0;
  18. }
  19. else
  20. {
  21. ER += 1.0 / R[i];
  22. }
  23. }
  24. }
  25. return 1.0/ER;
  26. }
  27.  
  28. void ShowEquivalentR(const std::vector<double> &R)
  29. {
  30. std::map<double/*等效電阻*/, unsigned/*組合方式*/> ER;
  31. unsigned n = 1 << R.size();
  32. for (unsigned sw = 1; sw < n; ++sw)
  33. {
  34. double er = CalcEquivalentR(R, sw);
  35. ER[er] = sw;
  36. }
  37.  
  38. for (std::map<double, unsigned>::iterator it = ER.begin(); it != ER.end(); ++it)
  39. {
  40.  
  41. std::cout << "when turn on(";
  42. unsigned sw = it->second;
  43. for (unsigned i = 0; i < R.size(); ++i)
  44. {
  45. if ((sw >> i) & 0x01)
  46. {
  47. std::cout << R[i] << ",";
  48. }
  49. }
  50. std::cout << ") the equal R is " << it->first << std::endl;
  51. }
  52. }
  53.  
  54. int main()
  55. {
  56. std::vector<double> R = {3.2,3.7,4.7,5.0}; // 修改 R
  57. ShowEquivalentR(R);
  58. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
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