在数组中查找平方和

时间:2013-11-26 21:04:53

标签: 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); //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;
}

3 个答案:

答案 0 :(得分:1)

我认为你过于复杂;你知道你只对区间[n..m]中的平方数感兴趣,所以只需计算它们:

for(i=n;i<=m/2;i++)
{
  int s = i*i;
  if(s<=m)
     squares[i] =s;
}

现在您已经在区间中拥有所有平方,只需计算所有总和。

答案 1 :(得分:0)

这样的事情:

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

检查范围内的每个数字,看它是否为方形。你可以倒退(类似于其他答案):

// Loop over every square.
for (int i=start; i<end/2; i++) {
  if (i*i < end) {
    for (int j=i+1; j<=end/2; j++) {
      if (j*j <= end) { 
        // Print the sum of these two squares.
        cout << i*i << "+" << j*j << "=" << i*i+j*j << endl;
      }
    }
  }
}

为了提高效率,您可能希望将i * i和j * j计算存储在变量中。

答案 2 :(得分:0)

我不理解人们发布的这些答案。这是一个不太糟糕的解决方案,当然不是超优化的,但它是可以理解的,并找到了所要求的答案。

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