C++ 求数组中的平方和

C++ 求数组中的平方和,c++,arrays,perfect-square,C++,Arrays,Perfect Square,任务是搜索数组中的平方数,并打印每两个平方的和。 例如,数组以1开头,以9结尾。首先,应该在数组中找到正方形。在这种情况下-1,4,9。每一个的总和是1+4=5,1+9=10,4+9=13。在我的程序中,当我输入起始值1和结束值9时,它只打印从1到9的数组。我不知道问题出在哪里 #include <iostream> #include <math.h> using namespace std; int arr[10][4]; bool sqrsum(int); //pr

任务是搜索数组中的平方数,并打印每两个平方的和。 例如,数组以1开头,以9结尾。首先,应该在数组中找到正方形。在这种情况下-1,4,9。每一个的总和是1+4=5,1+9=10,4+9=13。在我的程序中,当我输入起始值1和结束值9时,它只打印从1到9的数组。我不知道问题出在哪里

#include <iostream>
#include <math.h>
using namespace std;

int arr[10][4];
bool sqrsum(int); //prototype

int main (){
int n,m,j,a,b,x,y,finalpoint;
bool noresult;
cout << "Enter array starting value: " << endl;
cin >> n;
cout << "Enter array ending value: " << endl;
cin >> m;

if ((n<0)|| (m<0))
{
    cout << "Values cannot be negative." << endl;

}

if (n>m)
{
    j=n;
    n=m;
    m=j;
}
for (int i=n; i<=m; i++)
{
    if (sqrsum(i) == true)
    {
        for (int g=0; g<10; g++)
            if (arr[g][0] == -9)
        {
            finalpoint = g;
            break;
        }
    }
    if (finalpoint == 1)
    {
        cout << i << endl;
    }
    else
    {
        for (int g=0; g < (finalpoint / 2); g++)
        {
            cout << i << endl;
        }
    }
    noresult = false;
}
if (noresult == true)
{
    cout << "There is no valid square sum." << endl;
}
}


bool sqrsum(int i)
{
int arr1[101];
int x; // Last address
int z = 0;
bool rettrue = false;
//Function finding squares and their sum.
for (int j=0;j<10;j++)
{
    for (int k=0;k<4;k++)
    {
        arr[j][k] = -9;
    }
}
//Finding possible squares
for (int j=0; pow(j,2)<=i; j++)
{
    arr1[j] = pow(j,2);
    x = j;
}
//Cycles of sum
for (int j=0; j<=x; j++)
{
    for (int k=0; k<=x; k++)
    {
        if (arr1[j] + arr1[k] == i)
        {
            arr[z][0] = arr1[j];
            arr[z][1] = arr1[k];
            arr[z][2] = j;
            arr[z][3] = k;
            z++;
            rettrue=true;
        }
    }
}
if (rettrue == true)
    return true;
else
    return false;
}
#包括
#包括
使用名称空间std;
int arr[10][4];
布尔斯奎森(国际)//原型
int main(){
int n,m,j,a,b,x,y,finalpoint;
布尔·诺雷索;
cout n;
库特m;

如果((n我认为你把事情复杂化了;你知道你只对区间[n..m]中的平方数感兴趣,那么就计算它们:

for(i=n;i<=m/2;i++)
{
  int s = i*i;
  if(s<=m)
     squares[i] =s;
}
对于(i=n;i类似的内容:

// Loop over every value.
for (int i=start; i<end; i++) {
  if (!isSquare(i)) continue;
  // Sum this value with all values after it.
  for (int j=i+1; j<=end; j++) {
    if (!isSquare(j)) continue;
    // Print the sum of these two squares.
    cout << i << "+" << j << "=" << i+j << endl;
  }
}
//在每个值上循环。

对于(int i=start;i我不理解人们发布的这些答案。这里有一个解决方案不是很糟糕,当然不是超优化的,但它是可以理解的,可以找到所需的答案

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main()
{
    // find values that are squares in range [b,e]
    int b, e;

    // Get those values from user
    cout << "Enter array starting value: ";
    cin >> b;
    cout << "Enter array ending value: ";
    cin >> e;

    if(b<0 || e<0)
        cerr << "Values cannot be negative." << endl;

    if(e < b)
        cerr << "The starting value must be smaller or equal to the ending value." << endl;

    // Find all squares in range [b, e] and save them in a vector.
    vector<int> squares;
    for(int i = sqrt(b); i < e/2; ++i)
    {
        int possibleSquare = i*i;
        if(possibleSquare >= b && possibleSquare <= e)
            squares.push_back(possibleSquare);
    }

    // Output the sum of every pair in that vector of squares
    if(squares.empty())
    {
        cout << "There are no squares in [" << b << "," << e << "]" << endl;
        return 0;
    }

    cout << "\nEvery pairwise sum of every square in that range: " << endl;
    for(int i = 0; i < squares.size(); ++i)
        for(int j = i+1; j < squares.size(); ++j)
            cout << squares[i] + squares[j] << endl;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
//查找[b,e]范围内的平方值
int b,e;
//从用户处获取这些值
cout>b;
cout>e;

如果(b从观察到不良行为的地方开始,并向后查找计算或逻辑中的错误。在程序的关键点插入语句以输出值,以便您自己检查状态或使用调试器。此过程称为调试程序,是每个程序员必须学习的独立完成。进一步阅读:@paddy在这种情况下,反向工作可能有点高级,我建议彻底检查每一行的调试器。注意:显然,这段代码只能输出数字1-9,一次一行,有时一次,有时可能不止一次…
I
就是所有的输出。工作从一个
cout
后退并不先进。就像我说的:从观察到不良行为的地方开始。这个问题显示出完全没有努力。这个人清楚地写了一个完整的程序,运行了它,当它不起作用时感到惊讶。没有证据表明他们试图隔离和测试单个部分。@paddy Exa这就是为什么我不相信他们的技能来找出问题并从中逆向工作的原因…PS-编写代码一定是很努力的。显然这不是来自教程。我不明白你的意思,也许我太累了。我想我用If(…)涵盖了你提到的情况声明哦,很抱歉你提到的是我已经删除的评论,最后终于弄明白了。是的,已经涵盖了。让if和循环条件对齐对优雅来说是一场噩梦……注意:
I
应该从1开始,而不是从n开始。
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main()
{
    // find values that are squares in range [b,e]
    int b, e;

    // Get those values from user
    cout << "Enter array starting value: ";
    cin >> b;
    cout << "Enter array ending value: ";
    cin >> e;

    if(b<0 || e<0)
        cerr << "Values cannot be negative." << endl;

    if(e < b)
        cerr << "The starting value must be smaller or equal to the ending value." << endl;

    // Find all squares in range [b, e] and save them in a vector.
    vector<int> squares;
    for(int i = sqrt(b); i < e/2; ++i)
    {
        int possibleSquare = i*i;
        if(possibleSquare >= b && possibleSquare <= e)
            squares.push_back(possibleSquare);
    }

    // Output the sum of every pair in that vector of squares
    if(squares.empty())
    {
        cout << "There are no squares in [" << b << "," << e << "]" << endl;
        return 0;
    }

    cout << "\nEvery pairwise sum of every square in that range: " << endl;
    for(int i = 0; i < squares.size(); ++i)
        for(int j = i+1; j < squares.size(); ++j)
            cout << squares[i] + squares[j] << endl;
}