fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //ret に答えを入れてメイン関数に返す
  4. int solve() {
  5. int ret=0;
  6.  
  7. //第2回
  8. //モンスターの配列を作る
  9. int i;
  10. int Mhow,Mstats;
  11. int* Monsters;
  12.  
  13. printf("モンスターの数を入力して: \n");
  14. scanf("%d", &Mhow);//モンスターの数
  15. Monsters = (int*)malloc(sizeof(int) * Mhow);
  16. //エラー回避
  17. if (Monsters == NULL) {
  18. printf("game over\n");
  19. return 0;
  20. }
  21. for (i = 0; i < Mhow; i++) {
  22. printf("%d体目のモンスターの数値を入力して: \n",i+1);
  23. //Monsters[i] = scanf("%d", &Mstats);
  24. scanf("%d", &Mstats);
  25. Monsters[i] = Mstats;
  26. Mstats = 0;
  27. }
  28.  
  29. //ボールをどのモンスターに当てるか吟味する
  30. int j;
  31. int ball,Mmax=0;
  32.  
  33.  
  34. printf("ボールの数を入力して: \n");
  35. scanf("%d", &ball);//ボールの数
  36. while (ball > 0) {
  37. for (j = 0; j < Mhow; j++) {
  38. if (Monsters[j] > Monsters[Mmax])Mmax = j;
  39. }
  40. Monsters[Mmax] /= 2;
  41. ball--;
  42. Mmax = 0;
  43. }
  44.  
  45. //モンスターの数値の合計
  46. int k;
  47. for (k = 0; k < Mhow; k++) {
  48. ret += Monsters[k];
  49.  
  50. }
  51.  
  52.  
  53.  
  54. return ret;
  55. }
  56.  
  57. //メイン関数はいじらなくて良い
  58. int main(void) {
  59. printf("%d\n", solve());
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 5288KB
stdin
7
10 40 60 30 80 5 30
2
stdout
モンスターの数を入力して: 
1体目のモンスターの数値を入力して: 
2体目のモンスターの数値を入力して: 
3体目のモンスターの数値を入力して: 
4体目のモンスターの数値を入力して: 
5体目のモンスターの数値を入力して: 
6体目のモンスターの数値を入力して: 
7体目のモンスターの数値を入力して: 
ボールの数を入力して: 
185