fork download
  1. #include <stdio.h>
  2.  
  3.  
  4. int x;
  5.  
  6. //--------
  7. const int a_data[7][2] = {
  8. {9, 15},
  9. {12, 16},
  10. {16, 13},
  11. {18, 13},
  12. {31, 11},
  13. {32, 1},
  14. {36, 9} };
  15.  
  16. #define PILES_LIMIT 10
  17. #define ROWS_LIMIT 10//(STONES)
  18. #define NUMBER_OF_STONES 7
  19. #define NUMBER_OF_PILES 3
  20.  
  21.  
  22. int flg_finished;
  23. int lvl;
  24. int piles_cnt[PILES_LIMIT];
  25. int price_pile[PILES_LIMIT];
  26. int l;
  27. int total_pile_price[100];//da se uvelichat spored broq kamuni
  28. int kk;
  29. int min_price = 9999;// da se uvelichato spored broq kamuni
  30. int pile_position[PILES_LIMIT];
  31.  
  32. void manamana(int number_stones,int number_piles)
  33. {
  34.  
  35. while(flg_finished==0)
  36. {
  37. // add the next price to the current pile
  38. if(lvl != 0){price_pile[lvl] += a_data[piles_cnt[lvl]][1]*(a_data[piles_cnt[lvl]][0]-a_data[piles_cnt[lvl-1]][0]);}
  39. else {price_pile[lvl] += a_data[piles_cnt[lvl]][1]*(a_data[piles_cnt[lvl]][0]-a_data[0][0]);}
  40.  
  41.  
  42.  
  43. if(piles_cnt[lvl] < (number_stones - number_piles + lvl + 1) )//if this is not the current level maximum
  44. {
  45. piles_cnt[lvl]++;//increase the current level cnt
  46. if(lvl < number_piles - 1)//if this is not the max level go to the next level
  47. {
  48. lvl++;
  49. piles_cnt[lvl] = piles_cnt[lvl-1];//the next level cnt starts from this level cnt
  50. }
  51. else//stay at this level
  52. {
  53. if(piles_cnt[lvl] == (number_stones - number_piles + lvl + 1) )//if this is the last max calculate total price
  54. {
  55. for(l=0;l<NUMBER_OF_PILES;l++){total_pile_price[kk] += price_pile[l];}//get the total price
  56. x = total_pile_price[kk];
  57. printf("%d\n", x);
  58. if(min_price > total_pile_price[kk])
  59. {
  60. min_price = total_pile_price[kk];
  61. for(l=0;l<NUMBER_OF_PILES;l++)
  62. {
  63. pile_position[l+1] = piles_cnt[l]+1;
  64. }
  65. pile_position[0] = 1;
  66. }
  67.  
  68. kk++;
  69. }
  70.  
  71. }
  72. }
  73. else//if the current level maximum is reached go one level below
  74. {
  75. if(lvl != 0)
  76. {
  77. price_pile[lvl] = 0;//clearing the current level price
  78. lvl--;// go one level bellow
  79. }
  80. else {flg_finished = 1;}
  81. }
  82.  
  83. }
  84. lvl = 9000;
  85. }
  86.  
  87. int main(void) {
  88.  
  89. manamana(NUMBER_OF_STONES,NUMBER_OF_PILES);
  90.  
  91. printf("min_price:");
  92. printf("%d\n", min_price);
  93.  
  94. for(l=0;l<NUMBER_OF_PILES;l++)
  95. {
  96. printf("%d\n", pile_position[l] );
  97. }
  98. return 0;
  99. }
  100.  
  101.  
Success #stdin #stdout 0s 9432KB
stdin
1
2
10
42
11
stdout
387
371
176
375
359
367
120
275
255
185
318
296
292
257
498
min_price:120
1
3
5