寻找循环数组队列中的最小值

huangapple go评论72阅读模式
英文:

finding the minimum value in circular array queue

问题

public class CanadaTour {
    
    private CircularArrayQueue<City> cityQueue;
    private Map map;
    private City startCity;
    
    public CanadaTour(String fileName) {
        map = new Map();
        cityQueue = new CircularArrayQueue<City>();
        loadData(fileName);
    }
    
    private void loadData(String file) {
        MyFileReader reader = new MyFileReader(file);
        reader.readString(); // First line of headers.
        
        String cityName = null;
        int locX = 0;
        int locY = 0;
        double earnings = 0;
        int cityID = 0;
        
        while (!reader.endOfFile()) {
            cityName = reader.readString();
            locX = reader.readInt();
            locY = reader.readInt();
            earnings = reader.readDouble();
            cityID ++;
            
            City city = new City(cityID, cityName, locX, locY, earnings);
            
            if (cityID == 1) {
                startCity = city;
            }
            cityQueue.enqueue(city);
            map.addCity(city);
        }
    }
    
    public City findNextCity(City currCity, double currMoney) {
        double distance = 0;
        
        City result = cityQueue.dequeue();
        if (result != currCity || result.isMarkedInStack() || result.isMarkedOutOfStack()) {
            // add other conditionals
            
            distance = distBetweenCities(result, currCity);
            cityQueue.enqueue(result);
        }
        
        double distance1;
        for (int i = 1; i < cityQueue.getLength(); i++) {
            City result1 = cityQueue.dequeue();
            if (result1 != currCity || result1.isMarkedInStack() || result1.isMarkedOutOfStack()) {
                // add other conditionals
                
                distance1 = distBetweenCities(result1, currCity);
                if (distance1 < distance) {
                    distance = distance1;
                    return result1;
                }
                cityQueue.enqueue(result1);
            }
        }
        return result;
    }
    
    public double distBetweenCities(City city1, City city2) {
        double result = Math.sqrt(Math.pow(city2.getX() - city1.getX(), 2) +
                Math.pow(city2.getY() - city2.getY(), 2) * 1.0);
        return result;
    }
    
    public double calcFlightCost(double distance) {
        double flightCost;
        if (distance < 100.0) {
            flightCost = 127.00;
        } else {
            flightCost = (1.25 * distance) + 32.0;
        }
        return flightCost;
    }
}

Please note that the code you've provided seems to contain some logical errors, and it's not entirely clear what the "CircularArrayQueue" and "Map" classes are, as they are not included in your snippet. If you have further questions or need assistance with specific parts of this code, feel free to ask.

英文:
public class CanadaTour {
private CircularArrayQueue&lt;City&gt; cityQueue;
private Map map;
private City startCity;
public CanadaTour (String fileName) {
map = new Map();
cityQueue = new CircularArrayQueue&lt;City&gt;();
loadData(fileName);
}
private void loadData (String file) {
MyFileReader reader = new MyFileReader(file);
reader.readString(); // First line of headers.
String cityName = null;
int locX = 0;
int locY = 0;
double earnings = 0;
int cityID = 0;
while (!reader.endOfFile()) {
cityName = reader.readString();
locX = reader.readInt();
locY = reader.readInt();
earnings = reader.readDouble();
cityID ++;
City city = new City(cityID, cityName, locX, locY, earnings);
if (cityID == 1) {
startCity = city;
}
cityQueue.enqueue(city);
map.addCity(city);
}
}
public City findNextCity (City currCity, double currMoney) {
double distance = 0;
City result = cityQueue.dequeue();
if (result != currCity || result.isMarkedInStack() 
|| result.isMarkedOutOfStack())  //add other conditionals
distance = distBetweenCities(result, currCity);
cityQueue.enqueue(result);
double distance1;	
for (int i = 1; i &lt; cityQueue.getLength(); i ++) {
City result1 = cityQueue.dequeue();
if (result1 != currCity || result1.isMarkedInStack() 
|| result1.isMarkedOutOfStack()) { //add other conditionals
distance1 = distBetweenCities(result1, currCity);
if (distance1 &lt; distance) {
distance = distance1;
return result1;
}
cityQueue.enqueue(result1);
}
}
return result;		
}
public double distBetweenCities (City city1, City city2) {
double result =  Math.sqrt(Math.pow(city2.getX() - city1.getX(), 2) + 
Math.pow(city2.getY() - city2.getY(), 2) * 1.0);
return result;
}
public double calcFlightCost (double distance) {
double flightCost;
if (distance &lt; 100.0) {
flightCost = 127.00;
} else {
flightCost = (1.25 * distance) + 32.0;
}
return flightCost;
}

This is what I have thus far, but logically my answer seems wrong for the findNextCity method. and additionally, I don't even know how to approach the second part of the question (below).

I am supposed to go through each element in the cityQueue to determine which one is the
closest to the current city (from the first parameter) using the Euclidean distance
calculated in the next method (distBetweenCities). I must omit cities that already
marked in or out of the stack and the current city itself (otherwise a city will always be
the closest city to itself!). If the found city (with the smallest distance to current city) is
null, return null. Calculate the flight cost to this city and determine if it is affordable with
the band's current money. If so, return the city, but if it is not affordable then return null.

答案1

得分: 0

以下是翻译好的内容:

  • 你似乎正在出队和入队,以查看队列的头部。如果你的CircularArrayQueue实现了Queue,它应该有一个peek方法,可以查看头部而不移除它。
  • 在你的筛选条件中,或运算符应该是与(&&),如果我正确理解你的要求的话。
  • 正如你提到的,findNextCity中的逻辑似乎有点脆弱,它似乎在寻找第一个比下一个城市距离小的城市之类的东西。

你可能需要像这样的代码:

City closestCity = null;
for (City testCity: cityQueue) {
    if (testCity != currCity 
            && !testCity.isMarkedInStack() && !testCity.isMarkedOutOfStack()
            && (closestCity == null || distance(currCity, closestCity) > distance(currCity, testCity)))
        closestCity = testCity;
}
英文:

Without providing a complete solution, here are several pointers you might like to consider:

  • You seem to be dequeuing and enqueuing for the purpose of viewing the head of the queue. If your CircularArrayQueue implements Queue it should have a peek method that views the head without removing it.
  • The or operators in your filter condition should probably be and (&&) if I'm reading your requirements correctly
  • As you mention, your logic is pretty flaky in findNextCity it kinda seems to look for the first city that is smaller than the distance to the next city or something like that

You probably need something like:

City closestCity = null;
for (City testCity: cityQueue) {
if (testCity != currCity 
&amp;&amp; !testCity.isMarkedInStack() &amp;&amp; !testCity.isMarkedOutOfStack()
&amp;&amp; (closestCity == null || distance(currCity, closestCity) &gt; distance(currCity, testCity))
closestCity == testCity;
}

huangapple
  • 本文由 发表于 2020年7月23日 11:10:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63046278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定