#include <iostream>
#include <cmath>
#include <limits.h>
#include <stdlib.h>
using namespace std ;
long long NumberHouses = 0 ;
long long ClosestHouse[2] = {0,0} ;
long long TravelTimes = 0 ;
void GetHouses(long long NumH, long long House_X[], long long House_Y[])
{
for (long long a = 0 ; a < NumH ; a++)
{
cin >> House_X[a] ;
cin >> House_Y[a] ;
}
}
void SortArray(long long NumH, long long SortedArray[], long long HouseArray[])
{
long long InsertPoint = 0 ;
for (int a = 0 ; a < NumH ; a++)
{
SortedArray[a] = LONG_MAX ;
}
for (int a = 0 ; a < NumH ; a++)
{
for (int b = 0 ; b < NumH ; b++)
{
if (HouseArray[a] < SortedArray[b])
{
InsertPoint = b ;
for (int c = NumH ; c > b ; c--)
{
SortedArray[c] = SortedArray[c-1] ;
}
SortedArray[InsertPoint] = HouseArray[a] ;
b = NumH ;
}
}
}
}
void FindMedians(long long NumH, int NumMeds, long long SortedArray[], long long MediansArray[])
{
int MedianNum = 0 ;
long long *Sorted_X = new long long [NumH] ;
long long *Sorted_Y = new long long [NumH] ;
MedianNum = NumH/2 ;
if (NumMeds == 1)
{
MediansArray[0] = SortedArray[MedianNum] ;
}
else
{
MediansArray[0] = SortedArray[MedianNum] ;
MediansArray[1] = SortedArray[MedianNum + 1] ;
}
}
void FindMidHouse(long long NumH, int NumMeds, long long Cords_X[], long long Cords_Y[], long long Meds_X[], long long Meds_Y[])
{
long long *CloseHouses = new long long [NumH] ;
long long NumPoints = 0 ;
long long LowestMoves = LONG_MAX ;
for (int a = 0 ; a < NumH ; a++)
{
if (abs(Cords_X[a] - Meds_X[0]) > abs(Cords_X[a] - Cords_Y[0]))
{
if (abs(Cords_X[a] - Meds_X[0] <= LowestMoves))
{
LowestMoves = abs(Cords_X[a] - Meds_X[0]) ;
CloseHouses[a] = a ;
NumPoints++ ;
}
}
else
{
if (abs(Cords_Y[a] - Meds_Y[0] <= LowestMoves))
{
LowestMoves = abs(Cords_Y[a] - Meds_Y[0]) ;
CloseHouses[a] = a ;
NumPoints++ ;
}
}
}
long long *CloseHousesSums = new long long [NumH] ;
for (int a = 0 ; a < NumPoints ; a++)
{
for (int b = 0 ; b < NumH ; b++)
{
if ((abs(Cords_X[CloseHouses[a]]) - Cords_X[b]) > (abs(Cords_Y[CloseHouses[a]]) - Cords_Y[b]))
{
CloseHousesSums[a] = CloseHousesSums[a] + abs(Cords_X[CloseHouses[a]] - Cords_X[b]) ;
}
else
{
CloseHousesSums[a] = CloseHousesSums[a] + abs(Cords_Y[CloseHouses[a]] - Cords_Y[b]) ;
}
}
}
for (int a = 0 ; a < (NumPoints - 1) ; a++)
{
if (CloseHousesSums[a] < CloseHousesSums[a+1])
{
ClosestHouse[0] = Cords_X[CloseHouses[a]] ;
ClosestHouse[1] = Cords_Y[CloseHouses[a]] ;
}
}
}
void GetClosestHouse(long long NumH, long long House_X[], long long House_Y[], long long ClosestHouse[])
{
int MedianAmt = 0 ;
long long *Sorted_X = new long long [NumH] ;
long long *Sorted_Y = new long long [NumH] ;
/* long long *Sorted_1 = new long long ;//[NumH] ;
long long *Sorted_a = new long long ;//[NumH] ;
long long *Sorted_b = new long long ;//[NumH] ;
long long *Sorted_c = new long long ;//[NumH] ;
long long *Sorted_d = new long long ;//[NumH] ;
long long *Sorted_e = new long long ;//[NumH] ;
long long *Sorted_f = new long long ;//[NumH] ;*/
SortArray(NumberHouses, Sorted_X, House_X) ;
SortArray(NumberHouses, Sorted_Y, House_Y) ;
for (int a = 0 ; a < NumH ; a++)
{
cout << Sorted_X[a] << "," << Sorted_Y[a] << endl ;
}
if (NumH % 2 == 0)
{
MedianAmt = 1 ;
}
else
{
MedianAmt = 2 ;
}
cout << MedianAmt << endl ;
cout << "sun" ;
long long *Medians_X = new long long [MedianAmt] ;
long long *Medians_Y = new long long [MedianAmt] ;
cout << "shine" << endl ;
FindMedians(NumberHouses, MedianAmt, Sorted_X, Medians_X) ;
FindMedians(NumberHouses, MedianAmt, Sorted_Y, Medians_Y) ;
FindMidHouse(NumberHouses, MedianAmt, House_X, House_Y, Medians_X, Medians_Y) ;
}
void GetHouseDistances(long long NumH, long long House_X[], long long House_Y[], long long ClosestHouse[], long long& TravelTimes)
{
long long Difference_X = 0 ;
long long Difference_Y = 0 ;
for (int a = 0 ; a < NumH ; a++)
{
Difference_X = abs(ClosestHouse[0] - House_X[a]) ;
Difference_Y = abs(ClosestHouse[1] - House_Y[a]) ;
//cout << Difference_X << " - Difference_X" << endl ;
//cout << Difference_Y << " - Difference_Y" << endl ;
if (Difference_X > Difference_Y)
{
TravelTimes = TravelTimes + Difference_X ;
}
else
{
TravelTimes = TravelTimes + Difference_Y ;
}
}
//cout << TravelTimes << endl ;
}
int main()
{
cin >> NumberHouses ;
long long *House_X = new long long [NumberHouses] ; // x coordinate of houses
long long *House_Y = new long long [NumberHouses] ; // y coordinate of houses
GetHouses(NumberHouses, House_X, House_Y) ;
GetClosestHouse(NumberHouses, House_X, House_Y, ClosestHouse) ;
GetHouseDistances(NumberHouses, House_X, House_Y, ClosestHouse, TravelTimes) ;
cout << TravelTimes << endl ;
}
現在你會注意到我有一個很大的註釋掉了其他數組的代碼被創建了類似的代碼 - 當我有這些未註釋的時候 - 但是當我評論它時它崩潰了輸出'太陽'之後但'閃耀'之前。C++中的指針內存分配
我嘗試過使用其他參數來創建內存的長度 - 例如MedianAmt,這包括其他變量和實際數量。我也嘗試使用一個很長的變量作爲參數。
我不確定爲什麼它不起作用 - 如果您希望我提供有關我的代碼或情況的更多信息,我將盡我所能來回答他們 - 感謝您的幫助。
編輯:我運行的可執行文件,它得到'太陽',然後它凍結 - 它可能需要一兩秒鐘,然後它沒有響應並關閉。 NuMH可以達到很長的最大數量 - 64位我不知道我在哪裏釋放了任何內存 - 我是一個相對新手的程序員 - 並且沒有給出錯誤消息。我當時也認爲這是一個堆腐敗問題 - 但爲什麼要在所有超長的超長數組中添加我已註釋掉的數據導致它不會崩潰?
我可以發佈我的源代碼的其餘部分 - 但它是200行,可能有點凌亂 - 這是否有益?
編輯2:至於堆損壞我有我的IDE和程序保存在我的閃存驅動器 - 但試圖在兩臺不同的計算機上運行它 - 所以我認爲這是安全的,假設堆沒有損壞。也許一個指向內存的無效指針是問題,IDE可以影響我的指針去選擇內存的位置嗎?
編輯3:我在兩臺機器上運行windows 7,我嘗試運行。
編輯4:更新有我的完整源代碼..
當它「崩潰」時會發生什麼? – prelic 2012-03-06 21:08:23
你有什麼錯誤? NumH和MedianAmt有多大? – 2012-03-06 21:10:21
你在哪裏釋放內存?爲什麼你不使用矢量? – BatchyX 2012-03-06 21:10:33