C++编程更高效的做法

C++ programming more efficient way to do

这个程序是 运行ning,但我正在寻找一种更有效的方法来 运行 这个程序。 你可以看到我注释掉了代码,因为我认为它可以在没有额外代码的情况下运行。但是,我有点困惑。 在 else 语句中,我试图减少 x 值,使其变为 x=8 并且 运行s 再次循环并再次减少。 但它不起作用。 谁能帮我我该怎么办?谢谢。

#include <iostream>

#include <cmath>

using namespace std;

int main()
{
    int x, y;

    if (x = 9) {
        for (y = 0; y < 9; y++)
            if (3 * pow (x, 2) + 4* pow(y, 2) == 271)
                cout << "x value is : " << x << endl << "y value is: " << y<<endl;
            else {
                x--;

            }
    }
    /*   if (x = 8) {
        for (y = 0; y < 9; y++)
            if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
                cout << "x value is : " << x << endl << "y value is: " << y<<endl;
    }
     if (x = 7) {
        for (y = 0; y < 9; y++)
            if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
                cout << "x value is : " << x << endl << "y value is: " << y<<endl;
    }
     if (x = 6) {
        for (y = 0; y < 9; y++)
            if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
                cout << "x value is : " << x << endl << "y value is: " << y<<endl;
    }
     if (x = 5) {
        for (y = 0; y < 9; y++)
            if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
                cout << "x value is : " << x << endl << "y value is: " << y<<endl;
    }
     if (x = 4) {
        for (y = 0; y < 9; y++)
            if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
                cout << "x value is : " << x << endl << "y value is: " << y<<endl;
    }
     if (x = 3) {
         for (y = 0; y < 9; y++)
             if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
                 cout << "x value is : " << x << endl << "y value is: " << y<<endl;
    }
     if (x = 2) {
        for (y = 0; y < 9; y++)
            if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
                cout << "x value is : " << x << endl << "y value is: " << y<<endl;
    }
     if (x = 1) {
        for (y = 0; y < 9; y++)
            if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
                cout <<"x value is : "<< x <<endl<<"y value is: "<< y<<endl;
    }

    */

    system("pause");
    return 0;
}

只需使用 for 循环遍历 x:

的值
for (x = 9; x > 0; x--) {
    for (y = 0; y < 9; y++) {
        if (3 * pow (x, 2) + 4* pow(y, 2) == 271) {
            cout << "x: " << x << endl << "y: " << y << endl;
        }
    }
}