剩余银饰的重量(100%用例)D卷(Java&&Python&&C++&&&&C语言)

时间:2024-10-27 14:43:39

有 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版本

  1. import .*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner();
  5. int n = ();
  6. List<Integer> weights = new ArrayList<>();
  7. for (int i = 0; i < n; ++i) {
  8. int weight = ();
  9. (weight);
  10. }
  11. (process(weights));
  12. }
  13. public static int process(List<Integer> weights) {
  14. int res = 0;
  15. (weights);
  16. if (() == 1) {
  17. return (0);
  18. } else if (() == 2) {
  19. return ((0), (1));
  20. } else {
  21. while (() >= 3) {
  22. int z = (() - 1);
  23. (() - 1);
  24. int y = (() - 1);
  25. (() - 1);
  26. int x = (() - 1);
  27. (() - 1);
  28. int diff = ((z - y) - (y - x));
  29. if (diff == 0) {
  30. continue;
  31. }
  32. int index = 0;
  33. while (index < () && (index) < diff) {
  34. index++;
  35. }
  36. (index, diff);
  37. }
  38. if (() == 1) {
  39. res = (0);
  40. } else if (() == 2) {
  41. res = ((0), (1));
  42. }
  43. }
  44. return res;
  45. }
  46. }

python版本

  1. def process(weights):
  2. res = 0
  3. ()
  4. if len(weights) == 1:
  5. return weights[0]
  6. elif len(weights) == 2:
  7. return max(weights)
  8. while len(weights) >= 3:
  9. z = ()
  10. y = ()
  11. x = ()
  12. diff = abs((z - y) - (y - x))
  13. if diff == 0:
  14. continue
  15. index = 0
  16. while index < len(weights) and weights[index] < diff:
  17. index += 1
  18. (index, diff)
  19. if len(weights) == 1:
  20. res = weights[0]
  21. elif len(weights) == 2:
  22. res = max(weights)
  23. return res
  24. def main():
  25. n = int(input().strip())
  26. weights = list(map(int, input().strip().split()))
  27. print(process(weights))
  28. if __name__ == "__main__":
  29. main()

C++版本

  1. #include <iostream>
  2. #include <list>
  3. #include <algorithm>
  4. using namespace std;
  5. int process(list<int> &weights) {
  6. int res = 0;
  7. weights.sort();
  8. if (weights.size() == 1) {
  9. return weights.front();
  10. } else if (weights.size() == 2) {
  11. return max(weights.front(), weights.back());
  12. } else {
  13. while (weights.size() >= 3) {
  14. int z = weights.back();
  15. weights.pop_back();
  16. int y = weights.back();
  17. weights.pop_back();
  18. int x = weights.back();
  19. weights.pop_back();
  20. int diff = abs((z - y) - (y - x));
  21. if (diff == 0) {
  22. continue;
  23. }
  24. auto it = weights.begin();
  25. while (it != weights.end() && *it < diff) {
  26. ++it;
  27. }
  28. weights.insert(it, diff);
  29. }
  30. if (weights.size() == 1) {
  31. res = weights.front();
  32. } else if (weights.size() == 2) {
  33. res = max(weights.front(), weights.back());
  34. }
  35. }
  36. return res;
  37. }
  38. int main() {
  39. int n;
  40. cin >> n;
  41. list<int> weights;
  42. for (int i = 0; i < n; ++i) {
  43. int weight;
  44. cin >> weight;
  45. weights.push_back(weight);
  46. }
  47. cout << process(weights) << endl;
  48. return 0;
  49. }

C语言版本

  1. #include <>
  2. #include <>
  3. // Function to process the weights
  4. int process(int weights[], int n) {
  5. int res = 0;
  6. // Sort the weights array
  7. for (int i = 0; i < n - 1; ++i) {
  8. for (int j = 0; j < n - i - 1; ++j) {
  9. if (weights[j] > weights[j + 1]) {
  10. int temp = weights[j];
  11. weights[j] = weights[j + 1];
  12. weights[j + 1] = temp;
  13. }
  14. }
  15. }
  16. if (n == 1) {
  17. return weights[0];
  18. } else if (n == 2) {
  19. return weights[1];
  20. }
  21. // Process the weights array
  22. while (n >= 3) {
  23. int z = weights[--n];
  24. int y = weights[--n];
  25. int x = weights[--n];
  26. int diff = abs((z - y) - (y - x));
  27. if (diff == 0) {
  28. continue;
  29. }
  30. // Insert the diff back into the array
  31. int index = 0;
  32. while (index < n && weights[index] < diff) {
  33. index++;
  34. }
  35. // Shift elements to the right to make space for diff
  36. for (int i = n; i > index; --i) {
  37. weights[i] = weights[i - 1];
  38. }
  39. weights[index] = diff;
  40. n++;
  41. }
  42. if (n == 1) {
  43. res = weights[0];
  44. } else if (n == 2) {
  45. res = weights[1];
  46. }
  47. return res;
  48. }
  49. // Main function
  50. int main() {
  51. int n;
  52. scanf("%d", &n);
  53. int weights[n];
  54. for (int i = 0; i < n; ++i) {
  55. scanf("%d", &weights[i]);
  56. }
  57. int result = process(weights, n);
  58. printf("%d\n", result);
  59. return 0;
  60. }

版本

  1. function process(weights) {
  2. let res = 0;
  3. weights.sort((a, b) => a - b);
  4. if (weights.length === 1) {
  5. return weights[0];
  6. } else if (weights.length === 2) {
  7. return weights[1];
  8. }
  9. while (weights.length >= 3) {
  10. let z = weights.pop();
  11. let y = weights.pop();
  12. let x = weights.pop();
  13. let diff = Math.abs((z - y) - (y - x));
  14. if (diff === 0) {
  15. continue;
  16. }
  17. let index = 0;
  18. while (index < weights.length && weights[index] < diff) {
  19. index++;
  20. }
  21. weights.splice(index, 0, diff);
  22. }
  23. if (weights.length === 1) {
  24. res = weights[0];
  25. } else if (weights.length === 2) {
  26. res = weights[1];
  27. }
  28. return res;
  29. }
  30. function main() {
  31. const readline = require('readline');
  32. const rl = readline.createInterface({
  33. input: process.stdin,
  34. output: process.stdout
  35. });
  36. let n = 0;
  37. let weights = [];
  38. rl.on('line', (input) => {
  39. if (n === 0) {
  40. n = parseInt(input.trim());
  41. } else {
  42. weights = input.trim().split(' ').map(Number);
  43. rl.close();
  44. }
  45. });
  46. rl.on('close', () => {
  47. console.log(process(weights));
  48. });
  49. }
  50. main();