简体   繁体   English

程式无法正常运作

[英]Program doesn't work properly

so my program is supposed to work like this: 所以我的程序应该像这样工作:

  1. Input integers n and m (for example n = 1; m = 10) 输入整数n和m(例如n = 1; m = 10)
  2. Input all the values from n to m (for example 1;2;3;4;5;6;7;8;9;10) into a dynamic array 输入从n到m的所有值(例如1; 2; 3; 4; 5; 6; 7; 8; 9; 10)到动态数组中
  3. Then with bool function you have to search the dynamic array for 2 numbers who are squared and summed together and make some kind of 3rd number (for example 1*1 + 1*1 = 2; 2*2 + 2*2 = 8 etc etc) 然后使用bool函数,您必须在动态数组中搜索2个被平方和求和的数字,并得出某种第3个数字(例如1 * 1 + 1 * 1 = 2; 2 * 2 + 2 * 2 = 8等)等等)

The program for some reason either only outprints the very first combination or doesn't outprint anything at all (for example, if n = 1 and m = 10, it only outprints 1*1 + 1*1 = 2, but it should outprint 2*2 + 1*1 = 5; 2*2 + 2*2 = 8 etc etc, if n = 4 and m = 200 it doesn't even outprint anything). 出于某种原因,该程序要么只打印第一个组合,要么根本不打印任何内容(例如,如果n = 1且m = 10,则只打印1 * 1 + 1 * 1 = 2,但应该打印2 * 2 + 1 * 1 = 5; 2 * 2 + 2 * 2 = 8等等,如果n = 4且m = 200,它甚至都不会输出任何东西。 Where could the problem be? 问题可能在哪里? I've been stuck for hours and in my mind the program should work but it doesn't. 我已经呆了好几个小时了,在我的脑海中,该程序应该可以运行,但是没有。 Thanks a lot. 非常感谢。

    #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;
}

You need to spend more time breaking down the logic of your task. 您需要花费更多的时间来分解任务的逻辑。 The way you've worded it is confusing, and I think it's contributing to the way you've written your code. 您所说的方式令人困惑,我认为这有助于您编写代码。

  1. Input integers n and m (for example n = 1; m = 10) 输入整数n和m(例如n = 1; m = 10)
  2. Input all the values from n to m (for example 1;2;3;4;5;6;7;8;9;10) into a dynamic array 输入从n到m的所有值(例如1; 2; 3; 4; 5; 6; 7; 8; 9; 10)到动态数组中
  3. Then with bool function you have to search the dynamic array for 2 numbers who are squared and summed together and make some kind of 3rd number (for example 1*1 + 1*1 = 2; 2*2 + 2*2 = 8 etc etc) 然后使用bool函数,您必须在动态数组中搜索2个被平方和求和的数字,并得出某种第3个数字(例如1 * 1 + 1 * 1 = 2; 2 * 2 + 2 * 2 = 8等)等等)

I see no (logical) problem with how you've written part 1, but for clarity's sake, I'm going to rename them to start and end . 我对第1部分的编写方式没有任何(逻辑)问题,但是为了清楚起见,我将其重命名为startend

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

cin >> start;
cin >> end;

Part 2 is where we start hitting problems. 第2部分是我们开始遇到问题的地方。 For starters, you've made your program needlessly complicated by involving manual management of dynamic memory. 对于初学者,通过手动管理动态内存使程序变得不必要地复杂。 A std::vector is perfectly suited for our task (for more than one reason, as you'll soon see...) 一个std::vector非常适合我们的任务(有多个原因,您很快就会看到...)

std::vector<int> data;

We're going to load up our values into data . 我们将把我们的值加载到data Based on your prompt, the inputs for start and end are an inclusive range, so we'll write it like so: 根据您的提示, startend的输入是一个包含范围,因此我们将这样写:

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

In your version of the code, this is just confusing: 在您的代码版本中,这只是令人困惑:

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

You've defined size to be m - n , which will be 9, but the prompt wants all the numbers between 1 and 10, which means there should be 10 numbers; 您已将size定义为m - n ,将为9,但是提示希望所有1到10之间的数字,这意味着应该有10个数字。 so off the bat, you're going to have errors. 因此,马上就要出错了。 The code I've suggested will work more reliably. 我建议的代码将更可靠地工作。

Finally, let's consider Step 3. It can (and should) be broken down into several steps: 最后,让我们考虑步骤3。可以(并且应该)将其分解为几个步骤:

  • For each number in data ( z ), 对于dataz )中的每个数字,
    • iterate through each pair of numbers that preceded it (which, based on your examples, may include duplicates), and for each pair ( x and y ) 遍历其前面的每对数字(根据您的示例,这些数字可能包含重复项),并迭代每对数字( xy
    • determine if x*x + y*y == z , and if so, print those variables to the user. 确定x*x + y*y == z ,如果是,则将这些变量打印给用户。

So let's write that code. 因此,让我们编写该代码。

//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;
            }
        }
    }
}

For an input of 1 10 , I get the following output: 输入1 10 ,我得到以下输出:

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

And for an input of 4 200 I get the following output: 对于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