Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. void best_combination(int budget, int ff_cost, int hf_cost, int *final_ff_num, int *final_hf_num);
  2.  
  3. int main(void) {
  4. //toysQuestion();
  5. healthfoodQuestion();
  6. return 0;
  7. }
  8.  
  9. void healthfoodQuestion() {
  10. int ff_cost = 0, hf_cost = 0, budget = 0, final_ff_num = 0, final_hf_num = 0;
  11.  
  12. printf("Enter budget: $");
  13. scanf("%d", &budget);
  14. printf("Enter fast-food cost per meal: $");
  15. scanf("%d", &ff_cost);
  16. printf("Enter health-food cost per meal: $");
  17. scanf("%d", &hf_cost);
  18.  
  19. best_combination(budget, ff_cost, hf_cost, final_ff_num, final_hf_num);
  20. }
  21.  
  22. void best_combination(int budget, int ff_cost, int hf_cost, int final_ff_num, int final_hf_num)
  23. {
  24. int hf_array[100], ff_array[100], ff_temp = 0,
  25. hf_temp = 0, ff_counter = 0, hf_counter = 0,
  26. total = 0;
  27.  
  28. if (budget % (ff_cost + hf_cost) != 0)
  29. {
  30. for (int i = 0; i < budget; i++)
  31. {
  32. if (budget % (ff_cost + hf_cost) != 0) {
  33. if (ff_cost < hf_cost) {
  34. if (budget - hf_cost < hf_cost) {
  35. if ((budget - ff_cost) < (budget - hf_cost)) {
  36. final_ff_num++;
  37. budget -= ff_cost;
  38. }
  39. else {
  40. final_hf_num++;
  41. budget -= hf_cost;
  42. }
  43. }
  44.  
  45. final_ff_num++;
  46. budget -= ff_cost;
  47. }
  48. else {
  49. if (budget - ff_cost < ff_cost) {
  50. if ((budget - hf_cost) < (budget - ff_cost)) {
  51. final_hf_num++;
  52. budget -= hf_cost;
  53. }
  54. else {
  55. final_ff_num++;
  56. budget -= ff_cost;
  57. }
  58. }
  59.  
  60. final_hf_num++;
  61. budget -= hf_cost;
  62. }
  63. }
  64. }
  65. }
  66. else {
  67. for (int i = 1; hf_temp < budget; i++)
  68. {
  69. hf_array[i - 1] = hf_cost * i;
  70. hf_temp += hf_cost;
  71. hf_counter++;
  72. }
  73.  
  74. for (int i = 1; ff_temp < budget; i++)
  75. {
  76. ff_array[i - 1] = ff_cost * i;
  77. ff_temp += ff_cost;
  78. ff_counter++;
  79. }
  80.  
  81. for (int i = 0; i < hf_counter; i++)
  82. {
  83. for (int j = 0; j < ff_counter; j++)
  84. {
  85. total = hf_array[i] + ff_array[j];
  86.  
  87. if (total != budget) {
  88. total = 0;
  89. }
  90. else {
  91. if (i > j)
  92. {
  93. final_hf_num = ++i;
  94. final_ff_num = ++j;
  95. }
  96. }
  97. }
  98. }
  99. }
  100.  
  101. printf("Number of fast-food meals = %d \n", final_ff_num);
  102. printf("Number of health-food meals = %d \n", final_hf_num);
  103. printf("Remaining Budget = %d \n", budget);
  104. printf("\n");
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement