有 N 块二手市场收集的银饰,每块银饰的重量都是正整数,收集到的银饰会被熔化用于打造新的饰品。 每一回合,从中选出三块最重的银饰,然后一起熔掉。假设银饰的重量分别为 x 、y 和 z,且 x <= y <= z。那么熔掉的可能结果如下:
1. 如果 x == y == z,那么三块银饰都会被完全熔掉;
2. 如果 x == y 且 y != z,会剩余重量为 z - y 的银块无法被熔掉;
3. 如果 x != y 且 y == z,会剩余重量为 y - x 的银块无法被熔掉;
4. 如果 x != y 且 y != z,会剩余重量为 z - y 与 y - x 差值的银块无法被熔掉。
5. 最后,如果剩余两块,返回较大的重量(若两块重量相同,返回任意一块皆可);如果只剩 下一块,返回该块的重量;如果没有剩下,就返回 0。
输入描述:
输入数据为两行; 第一行为银饰数组长度 n,1 ≤ n ≤ 40; 第二行为 n 块银饰的重量,重量的取值范围为[1,2000],重量之间使用空格隔开;
输出描述:
如果剩余两块,返回较大的重量(若两块重量相同,返回任意一块皆可);如果只剩下一块,返回该块的重量;如果没有剩下,就返回 0。
示例1 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
3 1 1 1
输出
0
说明
说明:选出 1 1 1,得到 0,最终数组转换为 [],最后没有剩下银块,返回 0
示例2 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
3 3 7 10
输出
1
说明
选出 3 7 10,需要计算 (7-3) 和 (10-7) 的差值,即(7-3)-(10-7)=1,所以数组转换为 [1],剩余一块,返回该块重量,返回 1
java版本
-
import .*;
-
-
public class Main {
-
public static void main(String[] args) {
-
Scanner scanner = new Scanner();
-
int n = ();
-
-
List<Integer> weights = new ArrayList<>();
-
for (int i = 0; i < n; ++i) {
-
int weight = ();
-
(weight);
-
}
-
(process(weights));
-
}
-
-
public static int process(List<Integer> weights) {
-
int res = 0;
-
(weights);
-
if (() == 1) {
-
return (0);
-
} else if (() == 2) {
-
return ((0), (1));
-
} else {
-
while (() >= 3) {
-
int z = (() - 1);
-
(() - 1);
-
int y = (() - 1);
-
(() - 1);
-
int x = (() - 1);
-
(() - 1);
-
-
int diff = ((z - y) - (y - x));
-
-
if (diff == 0) {
-
continue;
-
}
-
-
int index = 0;
-
while (index < () && (index) < diff) {
-
index++;
-
}
-
(index, diff);
-
}
-
-
if (() == 1) {
-
res = (0);
-
} else if (() == 2) {
-
res = ((0), (1));
-
}
-
}
-
-
return res;
-
}
-
}
python版本
-
def process(weights):
-
res = 0
-
()
-
if len(weights) == 1:
-
return weights[0]
-
elif len(weights) == 2:
-
return max(weights)
-
-
while len(weights) >= 3:
-
z = ()
-
y = ()
-
x = ()
-
-
diff = abs((z - y) - (y - x))
-
-
if diff == 0:
-
continue
-
-
index = 0
-
while index < len(weights) and weights[index] < diff:
-
index += 1
-
(index, diff)
-
-
if len(weights) == 1:
-
res = weights[0]
-
elif len(weights) == 2:
-
res = max(weights)
-
-
return res
-
-
def main():
-
n = int(input().strip())
-
weights = list(map(int, input().strip().split()))
-
print(process(weights))
-
-
if __name__ == "__main__":
-
main()
C++版本
-
#include <iostream>
-
#include <list>
-
#include <algorithm>
-
-
using namespace std;
-
-
int process(list<int> &weights) {
-
int res = 0;
-
weights.sort();
-
if (weights.size() == 1) {
-
return weights.front();
-
} else if (weights.size() == 2) {
-
return max(weights.front(), weights.back());
-
} else {
-
while (weights.size() >= 3) {
-
int z = weights.back();
-
weights.pop_back();
-
int y = weights.back();
-
weights.pop_back();
-
int x = weights.back();
-
weights.pop_back();
-
-
int diff = abs((z - y) - (y - x));
-
-
if (diff == 0) {
-
continue;
-
}
-
auto it = weights.begin();
-
while (it != weights.end() && *it < diff) {
-
++it;
-
}
-
weights.insert(it, diff);
-
}
-
if (weights.size() == 1) {
-
res = weights.front();
-
} else if (weights.size() == 2) {
-
res = max(weights.front(), weights.back());
-
}
-
}
-
-
return res;
-
}
-
-
int main() {
-
int n;
-
cin >> n;
-
-
list<int> weights;
-
for (int i = 0; i < n; ++i) {
-
int weight;
-
cin >> weight;
-
weights.push_back(weight);
-
}
-
cout << process(weights) << endl;
-
return 0;
-
}
C语言版本
-
#include <>
-
#include <>
-
-
// Function to process the weights
-
int process(int weights[], int n) {
-
int res = 0;
-
// Sort the weights array
-
for (int i = 0; i < n - 1; ++i) {
-
for (int j = 0; j < n - i - 1; ++j) {
-
if (weights[j] > weights[j + 1]) {
-
int temp = weights[j];
-
weights[j] = weights[j + 1];
-
weights[j + 1] = temp;
-
}
-
}
-
}
-
-
if (n == 1) {
-
return weights[0];
-
} else if (n == 2) {
-
return weights[1];
-
}
-
-
// Process the weights array
-
while (n >= 3) {
-
int z = weights[--n];
-
int y = weights[--n];
-
int x = weights[--n];
-
-
int diff = abs((z - y) - (y - x));
-
-
if (diff == 0) {
-
continue;
-
}
-
-
// Insert the diff back into the array
-
int index = 0;
-
while (index < n && weights[index] < diff) {
-
index++;
-
}
-
-
// Shift elements to the right to make space for diff
-
for (int i = n; i > index; --i) {
-
weights[i] = weights[i - 1];
-
}
-
-
weights[index] = diff;
-
n++;
-
}
-
-
if (n == 1) {
-
res = weights[0];
-
} else if (n == 2) {
-
res = weights[1];
-
}
-
-
return res;
-
}
-
-
// Main function
-
int main() {
-
int n;
-
scanf("%d", &n);
-
-
int weights[n];
-
for (int i = 0; i < n; ++i) {
-
scanf("%d", &weights[i]);
-
}
-
-
int result = process(weights, n);
-
printf("%d\n", result);
-
-
return 0;
-
}
版本
-
function process(weights) {
-
let res = 0;
-
weights.sort((a, b) => a - b);
-
-
if (weights.length === 1) {
-
return weights[0];
-
} else if (weights.length === 2) {
-
return weights[1];
-
}
-
-
while (weights.length >= 3) {
-
let z = weights.pop();
-
let y = weights.pop();
-
let x = weights.pop();
-
-
let diff = Math.abs((z - y) - (y - x));
-
-
if (diff === 0) {
-
continue;
-
}
-
-
let index = 0;
-
while (index < weights.length && weights[index] < diff) {
-
index++;
-
}
-
weights.splice(index, 0, diff);
-
}
-
-
if (weights.length === 1) {
-
res = weights[0];
-
} else if (weights.length === 2) {
-
res = weights[1];
-
}
-
-
return res;
-
}
-
-
function main() {
-
const readline = require('readline');
-
const rl = readline.createInterface({
-
input: process.stdin,
-
output: process.stdout
-
});
-
-
let n = 0;
-
let weights = [];
-
-
rl.on('line', (input) => {
-
if (n === 0) {
-
n = parseInt(input.trim());
-
} else {
-
weights = input.trim().split(' ').map(Number);
-
rl.close();
-
}
-
});
-
-
rl.on('close', () => {
-
console.log(process(weights));
-
});
-
}
-
-
main();