fork download
  1. #include <stdio.h>
  2.  
  3. // 計算兩數的最大公因數
  4. int gcd(int a, int b) {
  5. while (b != 0) {
  6. int temp = b;
  7. b = a % b;
  8. a = temp;
  9. }
  10. return a;
  11. }
  12.  
  13. // 計算金屬塊的最大價值
  14. int value(int type, int width, int height, int length) {
  15. // 1. 檢查金屬類型是否有效
  16. if (type != 79 && type != 47 && type != 29 && type != 82 && type != 26 && type != 22) {
  17. return -1;
  18. }
  19.  
  20. // 2. 檢查長度、寬度、高度是否有效
  21. if (width <= 0 || height <= 0 || length <= 0) {
  22. return -2;
  23. }
  24.  
  25. // 3. 計算最大正方體的邊長
  26. int side = gcd(width, height);
  27. side = gcd(side, length);
  28.  
  29. // 4. 根據金屬類型設置單位價值
  30. int value_per_unit;
  31. if (type == 79) {
  32. value_per_unit = 30; // 金
  33. } else if (type == 47) {
  34. value_per_unit = 10; // 銀
  35. } else if (type == 29) {
  36. value_per_unit = 4; // 銅
  37. } else if (type == 82) {
  38. value_per_unit = 5; // 鉛
  39. } else if (type == 26) {
  40. value_per_unit = 3; // 鐵
  41. } else {
  42. value_per_unit = 9; // 鈦
  43. }
  44.  
  45. // 5. 計算並回傳金屬塊的最大價值
  46. int volume = side * side * side;
  47. return value_per_unit * volume * volume;
  48. }
  49.  
  50. int main() {
  51. int type, width, height, length;
  52. scanf("%d%d%d%d", &type, &width, &height, &length);
  53. printf("%d", value(type, width, height, length));
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 5272KB
stdin
79 4 8 2
stdout
1920