【问题标题】:Finding square sum in array [closed]在数组中查找平方和[关闭]
【发布时间】:2013-12-12 05:40:56
【问题描述】:

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

【问题讨论】:

  • 从你观察到不良行为的地方开始,向后工作,寻找计算或逻辑中的错误。在程序的关键点插入语句以输出值,以便您可以自己检查状态,或使用调试器。这个过程称为调试程序,是每个程序员必须学会独立完成的事情。
  • @paddy 在这种情况下向后工作可能有点先进,我建议对每一行的调试器进行彻底的逐步检查。注意:显然这段代码只能输出数字 1-9,一次一行,有时一次,有时可能不止一次……i 就是所有的输出。
  • cout 向后工作并不先进。就像我说的:从观察到不良行为的地方开始。这个问题表明绝对没有努力。这个人清楚地写了一个完整的程序,运行它,当它不起作用时感到惊讶。没有证据表明他们试图隔离和测试单个部件。
  • @paddy 正是为什么我不相信他们的技能来查明问题并从问题中逆向工作...... PS - 编写代码必须付出一些努力。显然这不是来自教程。

标签: c++ arrays perfect-square


【解决方案1】:

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

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

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

【讨论】:

  • 我不明白你的意思,也许我太累了。我想我用 If(..) 语句涵盖了你提到的情况
  • 哦,对不起,你指的是我已经删除的评论,终于明白了。是的,它被覆盖了。让 if 和循环条件排列是优雅的噩梦......注意:i 应该从 1 开始,而不是从 n 开始。
【解决方案2】:

类似这样的:

// 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 计算存储在一个变量中。

【讨论】:

    【解决方案3】:

    我不理解人们发布的这些答案。这是一个还不错的解决方案,当然不是超优化的,但它是可以理解的,并且可以找到所需的答案。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      相关资源
      最近更新 更多