我想根据价格矩阵计算每个车站之间的票价:
a b c
a 0 2 3
b 4 0 1
c 7 2 0示例:基于上述价格矩阵中的值的from a to b print 2或from c to a print 7。
这里,我想打印基于两个车站列表的火车票票价:"from:“列表和" to :”列表。我想在比较后打印票价。每种组合都有固定的票价。例如a站到b站,票价是10美元。任何一个站到其他站都有固定的票价。
发布于 2011-07-04 02:06:20
我将创建一个类,它负责存储票价。
public class FareStorage {
Map<TownCombination, Double> fares;
//...
public double getFare(String townA, String townB) {
return fares.get(new TownCombination(townA, townB));
}
public void addFare(String townA, String townB, double fare) {
fares.put(new TownCombination(townA, townB));
}
class TownCombination {
String town1;
String town2;
//If a fare from A to B is equals the fare from B to A,
//then the A-B and the B-A combinations should be equal.
//Override hashCode and equals the way you want.
}
}它还不完整,但我希望您能理解。下面是你使用它的方法:
FareStorage storage = new FareStorage();
storage.addFare("A", "B", 10.2);
//....
double fare = storage.get("A", "B");发布于 2011-07-04 13:26:05
您也可以使用guava's Table。
示例:
Table<Integer, String, String> table = HashBasedTable.create();
table.put(1, "a", "1a");
table.put(1, "b", "1b");
table.put(2, "a", "2a");
table.put(2, "b", "2b");https://stackoverflow.com/questions/6564271
复制相似问题