这里使用javascript演示,省去输入步骤
//骑士定义
var knight = function (no, fighting, hate) {
this.no = no;
this.fighting = fighting;
this.hate = hate;
this.del=false;
return this;
}
//骑士图鉴
var knights = [
new knight(1,10, 2),
new knight(2,20, 3),
new knight(3,30, 1),
//new knight(4,40, 1),
//new knight(5,20,4)
];
//计算最大战斗力
function maxFighting() {
quickSort(knights);
for (var i = 0; i < knights.length; i++) {
var a = knights[i];
if (a.del) {
continue;
}
for (var j = 0; j < knights.length; j++) {
var b = knights[j];
if (b.del) {
continue;
}
if (a.hate == b.no) {
knights[i].del = true;
break;
}
}
}
var totalFighting = 0;
for (var i = 0; i < knights.length; i++) {
var knight=knights[i];
if (!knight.del) {
totalFighting += knight.fighting;
}
}
return totalFighting;
}
//排序
function quickSort(ary) {
for (var i = 0; i < ary.length; i++) {
for (var j = i + 1; j < ary.length; j++) {
if (ary[i].fighting > ary[j].fighting) {
var temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}
}
}
}