fork download
  1. #include <stdio.h>
  2.  
  3. // value 函式實作
  4. int value(int type, int width, int height, int length) {
  5. // 金屬對應的價值表
  6. int metal_values[6] = {30, 10, 4, 5, 3, 9}; // 金、銀、銅、鉛、鐵、鈦
  7. int metal_types[6] = {79, 47, 29, 82, 26, 22}; // 金屬類型代號
  8.  
  9. // 驗證金屬類型
  10. int metal_index = -1;
  11. for (int i = 0; i < 6; i++) {
  12. if (type == metal_types[i]) {
  13. metal_index = i;
  14. break;
  15. }
  16. }
  17. if (metal_index == -1) {
  18. return -1; // 無效的金屬類型
  19. }
  20.  
  21. // 驗證長、寬、高是否有效
  22. if (width <= 0 || height <= 0 || length <= 0) {
  23. return -2; // 無效的尺寸
  24. }
  25.  
  26. // 計算最大的正方體邊長
  27. int min_dimension = width;
  28. if (height < min_dimension) min_dimension = height;
  29. if (length < min_dimension) min_dimension = length;
  30.  
  31. // 從最大可能的邊長開始嘗試
  32. int max_cube_size = 0;
  33. for (int size = min_dimension; size > 0; size--) {
  34. if (width % size == 0 && height % size == 0 && length % size == 0) {
  35. max_cube_size = size;
  36. break;
  37. }
  38. }
  39.  
  40. // 計算正方體的體積、單個正方體的價值,以及總價值
  41. int cube_volume = max_cube_size * max_cube_size * max_cube_size;
  42. int cube_value = cube_volume * cube_volume * metal_values[metal_index];
  43. int total_cubes = (width / max_cube_size) * (height / max_cube_size) * (length / max_cube_size);
  44. int total_value = cube_value * total_cubes;
  45.  
  46. return total_value;
  47. }
  48.  
  49. int main() {
  50. int type, width, height, length;
  51. scanf("%d%d%d%d", &type, &width, &height, &length);
  52. printf("%d", value(type, width, height, length));
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 5280KB
stdin
47 -3 18 81
stdout
-2