-1

任务是在数组中搜索平方数并打印每两个平方数的总和。例如数组以 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;
}
4

3 回答 3

1

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

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

现在你已经有了区间中的所有平方,只需计算所有的总和。

于 2013-11-26T21:21:02.920 回答
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;
}
于 2013-11-26T22:01:30.327 回答
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 计算存储在一个变量中。

于 2013-11-26T21:22:58.153 回答