[英]Program doesn't work properly
所以我的程序應該像這樣工作:
出於某種原因,該程序要么只打印第一個組合,要么根本不打印任何內容(例如,如果n = 1且m = 10,則只打印1 * 1 + 1 * 1 = 2,但應該打印2 * 2 + 1 * 1 = 5; 2 * 2 + 2 * 2 = 8等等,如果n = 4且m = 200,它甚至都不會輸出任何東西。 問題可能在哪里? 我已經呆了好幾個小時了,在我的腦海中,該程序應該可以運行,但是沒有。 非常感謝。
#include <iostream>
using namespace std;
bool isSquared (int i){
// bool result = false;
//int div;
//int *squares = new int [i];
for (int j=1;j<=i;j++){
for (int k=1;k<=i;k++){
if (k*k + j*j == i) return true;
else return false;
}
}
}
int main()
{
int n,m,i,size;
cin >>n;
cin >>m;
size = m - n;
int *real = new int [m - n];
for (int q=0, j=n; q<size, j<=m; q++, j++){
real[q] = j;
}
for (int q=0; q<=size; q++){
cout <<real[q] <<" | ";
}
cout <<endl;
for (int i=n; i<=m; i++){
if (isSquared(i) == true){
for (int j=0; j<=size; j++){
for (int k=0; k<=size; k++){
if (real[j]*real[j] + real[k]*real[k] == i){
cout <<i <<"=" <<real[j] <<"*" <<real[j] <<"+" <<real[k] <<"*" <<real[k] <<endl;
}
}
}
}
}
return 0;
}
您需要花費更多的時間來分解任務的邏輯。 您所說的方式令人困惑,我認為這有助於您編寫代碼。
- 輸入整數n和m(例如n = 1; m = 10)
- 輸入從n到m的所有值(例如1; 2; 3; 4; 5; 6; 7; 8; 9; 10)到動態數組中
- 然后使用bool函數,您必須在動態數組中搜索2個被平方和求和的數字,並得出某種第3個數字(例如1 * 1 + 1 * 1 = 2; 2 * 2 + 2 * 2 = 8等)等等)
我對第1部分的編寫方式沒有任何(邏輯)問題,但是為了清楚起見,我將其重命名為start
和end
。
//I'm also getting rid of superfluous variables we don't need.
int start, end;
cin >> start;
cin >> end;
第2部分是我們開始遇到問題的地方。 對於初學者,通過手動管理動態內存使程序變得不必要地復雜。 一個std::vector
非常適合我們的任務(有多個原因,您很快就會看到...)
std::vector<int> data;
我們將把我們的值加載到data
。 根據您的提示, start
和end
的輸入是一個包含范圍,因此我們將這樣寫:
for(int i = start; i <= end; i++) {
data.emplace_back(i);
}
在您的代碼版本中,這只是令人困惑:
for (int q=0, j=n; q<size, j<=m; q++, j++){
real[q] = j;
}
您已將size
定義為m - n
,將為9,但是提示希望所有1到10之間的數字,這意味着應該有10個數字。 因此,馬上就要出錯了。 我建議的代碼將更可靠地工作。
最后,讓我們考慮步驟3。可以(並且應該)將其分解為幾個步驟:
data
( z
)中的每個數字,
x
和y
) x*x + y*y == z
,如果是,則將這些變量打印給用戶。 因此,讓我們編寫該代碼。
//Note we're using data.size(), to ensure we stay in valid memory
for(int i = 0; i < data.size(); i++) {
int z = data[i];
//You can change the checks for j and k to "j <= i" and "k <= i" respectively
//if you want solutions where z*z + z*z = z, which can happen if z == 0.
for(int j = 0; j < i; j++) {
//We don't need to repeat identical pairs, so we're starting k at j.
for(int k = j; k < i; k++) {
int x = data[j];
int y = data[k];
if(x*x + y*y == z) {
std::cout
<< x << "*" << x
<< " + "
<< y << "*" << y
<< " = "
<< z
<< std::endl;
}
}
}
}
輸入1 10
,我得到以下輸出:
1*1 + 1*1 = 2
1*1 + 2*2 = 5
2*2 + 2*2 = 8
1*1 + 3*3 = 10
對於4 200
的輸入,我得到以下輸出:
4*4 + 4*4 = 32
4*4 + 5*5 = 41
5*5 + 5*5 = 50
4*4 + 6*6 = 52
5*5 + 6*6 = 61
4*4 + 7*7 = 65
6*6 + 6*6 = 72
5*5 + 7*7 = 74
4*4 + 8*8 = 80
6*6 + 7*7 = 85
5*5 + 8*8 = 89
4*4 + 9*9 = 97
7*7 + 7*7 = 98
6*6 + 8*8 = 100
5*5 + 9*9 = 106
7*7 + 8*8 = 113
4*4 + 10*10 = 116
6*6 + 9*9 = 117
5*5 + 10*10 = 125
8*8 + 8*8 = 128
7*7 + 9*9 = 130
6*6 + 10*10 = 136
4*4 + 11*11 = 137
8*8 + 9*9 = 145
5*5 + 11*11 = 146
7*7 + 10*10 = 149
6*6 + 11*11 = 157
4*4 + 12*12 = 160
9*9 + 9*9 = 162
8*8 + 10*10 = 164
5*5 + 12*12 = 169
7*7 + 11*11 = 170
6*6 + 12*12 = 180
9*9 + 10*10 = 181
4*4 + 13*13 = 185
8*8 + 11*11 = 185
7*7 + 12*12 = 193
5*5 + 13*13 = 194
10*10 + 10*10 = 200
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.