所以我的程序中有整数m
和n
,输入值后应创建一个值为m
到n
的数组(对于{例如m = 1
和n = 10
,它创建数组q
,其值为1到10)。然后它在数组中查看是否有任何数字等于平方的任意两个数和(例如,在数组中,数字2等于1平方+ 1平方)。问题是程序只给出了第一个找到的结果,作为一个例子让我们再次取1和10,它只打印2 = 1 * 1 + 1 * 1,它应该再次通过for循环并找出那个5 = 2 * 2 + 1 * 1等等。甚至可以使用以int开头的函数吗?
#include <iostream>
using namespace std;
int squared (int m, int n, int* M)
{
int result;
for(int i= 0; i < n; i++)
for(int j= 0; j < n; j++)
for(int k= 0; k < n; k++)
if( (M[i] == ( (M[j] * M[j]) + (M[k] * M[k]) )))
{
result = ( (M[j] * M[j]) + (M[k] * M[k]) );
cout << result << endl;
return result; // if success we return result
}
return -1;
}
int main ()
{
int n, m, size;
do
{
cout <<"m: ";
cin >> m;
cout << "n: ";
cin >> n;
if(n <= 0 || m <= 0)
cout <<"You can't input an integer that is 0 or below: ";
if(m >= n)
cout << "n must be greater than m!" << endl;
}while (m <= 0 || n <= 0 || m >= n);
size = n - m;
int* M = new int[n - m];
for(int i= 0, j = m; i <= size; i++, j++)
M[i] = j;
for(int i = 0; i <= size; i++)
cout << M[i] << ", " ;
cout << endl;
cout << squared(m, n, M) << endl;
delete[] M;
return 0;
}