繁体   English   中英

程式无法正常运作

[英]Program doesn't work properly

所以我的程序应该像这样工作:

  1. 输入整数n和m(例如n = 1; m = 10)
  2. 输入从n到m的所有值(例如1; 2; 3; 4; 5; 6; 7; 8; 9; 10)到动态数组中
  3. 然后使用bool函数,您必须在动态数组中搜索2个被平方和求和的数字,并得出某种第3个数字(例如1 * 1 + 1 * 1 = 2; 2 * 2 + 2 * 2 = 8等)等等)

出于某种原因,该程序要么只打印第一个组合,要么根本不打印任何内容(例如,如果n = 1且m = 10,则只打印1 * 1 + 1 * 1 = 2,但应该打印2 * 2 + 1 * 1 = 5; 2 * 2 + 2 * 2 = 8等等,如果n = 4且m = 200,它甚至都不会输出任何东西。 问题可能在哪里? 我已经呆了好几个小时了,在我的脑海中,该程序应该可以运行,但是没有。 非常感谢。

    #include <iostream>

using namespace std;

bool isSquared (int i){
   // bool result = false;

    //int div;
    //int *squares = new int [i];

    for (int j=1;j<=i;j++){
        for (int k=1;k<=i;k++){
            if (k*k + j*j == i) return true;
            else return false;
        }
    }
}


int main()
{
    int n,m,i,size;

    cin >>n;
    cin >>m;

    size = m - n;
    int *real = new int [m - n];

    for (int q=0, j=n; q<size, j<=m; q++, j++){
        real[q] = j;
    }

    for (int q=0; q<=size; q++){
        cout <<real[q] <<" | ";
    }

    cout <<endl;

    for (int i=n; i<=m; i++){
        if (isSquared(i) == true){
            for (int j=0; j<=size; j++){
                for (int k=0; k<=size; k++){
                    if (real[j]*real[j] + real[k]*real[k] == i){
                        cout <<i <<"=" <<real[j] <<"*" <<real[j] <<"+" <<real[k] <<"*" <<real[k] <<endl;
                    }
                }

            }


        }
    }

    return 0;
}

您需要花费更多的时间来分解任务的逻辑。 您所说的方式令人困惑,我认为这有助于您编写代码。

  1. 输入整数n和m(例如n = 1; m = 10)
  2. 输入从n到m的所有值(例如1; 2; 3; 4; 5; 6; 7; 8; 9; 10)到动态数组中
  3. 然后使用bool函数,您必须在动态数组中搜索2个被平方和求和的数字,并得出某种第3个数字(例如1 * 1 + 1 * 1 = 2; 2 * 2 + 2 * 2 = 8等)等等)

我对第1部分的编写方式没有任何(逻辑)问题,但是为了清楚起见,我将其重命名为startend

//I'm also getting rid of superfluous variables we don't need.
int start, end;

cin >> start;
cin >> end;

第2部分是我们开始遇到问题的地方。 对于初学者,通过手动管理动态内存使程序变得不必要地复杂。 一个std::vector非常适合我们的任务(有多个原因,您很快就会看到...)

std::vector<int> data;

我们将把我们的值加载到data 根据您的提示, startend的输入是一个包含范围,因此我们将这样写:

for(int i = start; i <= end; i++) {
    data.emplace_back(i);
}

在您的代码版本中,这只是令人困惑:

for (int q=0, j=n; q<size, j<=m; q++, j++){
    real[q] = j;
}

您已将size定义为m - n ,将为9,但是提示希望所有1到10之间的数字,这意味着应该有10个数字。 因此,马上就要出错了。 我建议的代码将更可靠地工作。

最后,让我们考虑步骤3。可以(并且应该)将其分解为几个步骤:

  • 对于dataz )中的每个数字,
    • 遍历其前面的每对数字(根据您的示例,这些数字可能包含重复项),并迭代每对数字( xy
    • 确定x*x + y*y == z ,如果是,则将这些变量打印给用户。

因此,让我们编写该代码。

//Note we're using data.size(), to ensure we stay in valid memory
for(int i = 0; i < data.size(); i++) {
    int z = data[i];
    //You can change the checks for j and k to "j <= i" and "k <= i" respectively
    //if you want solutions where z*z + z*z = z, which can happen if z == 0.
    for(int j = 0; j < i; j++) {
        //We don't need to repeat identical pairs, so we're starting k at j.
        for(int k = j; k < i; k++) {
            int x = data[j];
            int y = data[k];
            if(x*x + y*y == z) {
                std::cout 
                << x << "*" << x 
                << " + "
                << y << "*" << y
                << " = "
                << z
                << std::endl;
            }
        }
    }
}

输入1 10 ,我得到以下输出:

1*1 + 1*1 = 2
1*1 + 2*2 = 5
2*2 + 2*2 = 8
1*1 + 3*3 = 10

对于4 200的输入,我得到以下输出:

4*4 + 4*4 = 32
4*4 + 5*5 = 41
5*5 + 5*5 = 50
4*4 + 6*6 = 52
5*5 + 6*6 = 61
4*4 + 7*7 = 65
6*6 + 6*6 = 72
5*5 + 7*7 = 74
4*4 + 8*8 = 80
6*6 + 7*7 = 85
5*5 + 8*8 = 89
4*4 + 9*9 = 97
7*7 + 7*7 = 98
6*6 + 8*8 = 100
5*5 + 9*9 = 106
7*7 + 8*8 = 113
4*4 + 10*10 = 116
6*6 + 9*9 = 117
5*5 + 10*10 = 125
8*8 + 8*8 = 128
7*7 + 9*9 = 130
6*6 + 10*10 = 136
4*4 + 11*11 = 137
8*8 + 9*9 = 145
5*5 + 11*11 = 146
7*7 + 10*10 = 149
6*6 + 11*11 = 157
4*4 + 12*12 = 160
9*9 + 9*9 = 162
8*8 + 10*10 = 164
5*5 + 12*12 = 169
7*7 + 11*11 = 170
6*6 + 12*12 = 180
9*9 + 10*10 = 181
4*4 + 13*13 = 185
8*8 + 11*11 = 185
7*7 + 12*12 = 193
5*5 + 13*13 = 194
10*10 + 10*10 = 200

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM