sách gpt4 ai đã đi

c++ nhận được giá trị âm khi cộng hai giá trị tuyệt đối (dương)

In lại 作者:行者123 更新时间:2023-11-30 03:25:48 28 4
mua khóa gpt4 Nike

我遇到了一个奇怪的问题。添加两个绝对(正)值时,我得到负值。我正在尝试通过编码游戏解决此练习(链接:https://www.codingame.com/ide/puzzle/network-cabling).

我试过调试,我得到了这个:

X[i]: 19715507 X[j]: 938059973 // This one is good
temp: 918344466

Y[i]: 470868309 Y[j]: -816049599 //Something is wrong here
temp: -2089704922

这段代码可能不是练习的好解决方案,我需要对其进行改进,但我仍然无法弄清楚为什么该值为负数。

请帮忙。

Cảm ơn.

Mã số:

int chính()
{

int N;
int X[100000];
int Y[100000];

cin >> N; cin.ignore();
for (int i = 0; i < N; i++) { //reading all input

cin >> X[i] >> Y[i]; cin.ignore();
}

int ilgis=0; //ilgis means length

for(int i=0; i
//so it could find a lower value later
// "not the best solution"

int min=99999999999999999999; //shortest distance between houses
for(int j=0; j
// distance from one house to another

if(j!=i){ //I can't count distance from the same house because
// it would be 0

int temp=0;
//counting the distance differently if the
//value is negative or positive

if(X[i]<=0&&X[j]<=0) temp+=abs(abs(X[i])-abs(X[j]));
else if(X[i]<=0&&X[j]>=0) temp+=abs(X[i])+abs(X[j]);
else if(X[i]>=0&&X[j]>=0) temp+=abs(X[i]-X[j]);
else if(X[i]>=0&&X[j]<=0) temp+=(X[i])+abs(X[j]);

//same with y axis

if(Y[i]<=0&&Y[j]<=0) temp+=abs(abs(Y[i])-abs(Y[j]));
else if(Y[i]<=0&&Y[j]>=0) temp+=abs(Y[i])+abs(Y[j]);
else if(Y[i]>=0&&Y[j]>=0) temp+=abs(Y[i]-Y[j]);
else if(Y[i]>=0&&Y[j]<=0) temp+=(Y[i])+abs(Y[j]);

if(min>temp) min=temp;
}
} //if i found the shortesst distance between
//houses I add that value to overall distance
// and continue until all houses are checked
ilgis+=min;

}
cout<<><>
}

练习的输入:

 8
-28189131 593661218
102460950 1038903636
938059973 -816049599
-334087877 -290840615
842560881 -116496866
-416604701 690825290
19715507 470868309
846505116 -694479954

28 4 0
Bài viết được đề xuất: C++,构造函数,析构函数
Bài viết được đề xuất: c++ - 正则表达式捕获使代码崩溃
Bài viết được đề xuất: c++ - boost 语言环境不完整类型 boundary_indexing
Bài viết được đề xuất: C++ 半偶数四舍五入到 x 位数
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com