5X + 2Y +Z = 50 的所有非负整数解

这种题的解题方法都差不多,不停的循环,不过如果做一下细分,效率应该可以提升很多,下面把最常规效率也最低的代码贴上,有时间再优化

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int x, y, z;
 6 
 7     for (x = 0; x <= 10; x++ )
 8     {
 9         for ( y = 0; y <= 25; y++)
10         {
11             for ( z = 0; z <= 50; z++)
12             {
13                 if ((5 * x + 2 * y + z) == 50)
14                 {
15                     cout<<"x = "<<x <<" , y = "<<y<<" , z = "<<z<<endl; 
16                 }
17             }
18         }
19     }
20 
21     return 0;
22 }
原文地址:https://www.cnblogs.com/leisc/p/3840507.html