Với ba giá trị sau đây, nhiệm vụ là tìm tổng số lượng sôcôla tối đa bạn có thể ăn. & NBSP;
- Tiền: Tiền bạn phải mua sôcôla
- Giá: Giá sô cô la
- Gói: Số lượng gói được trả lại để có thêm một sô cô la.
Có thể giả định rằng tất cả các giá trị đã cho là số nguyên dương và lớn hơn 1.Examples: & nbsp; & nbsp;
Examples:
Input: money = 16, price = 2, wrap = 2 Output: 15 Price of a chocolate is 2. You can buy 8 chocolates from amount 16. You can return 8 wrappers back and get 4 more chocolates. Then you can return 4 wrappers and get 2 more chocolates. Finally you can return 2 wrappers to get 1 more chocolate. Input: money = 15, price = 1, wrap = 3 Output: 22 We buy and eat 15 chocolates We return 15 wrappers and get 5 more chocolates. We return 3 wrappers, get 1 chocolate and eat it [keep 2 wrappers]. Now we have 3 wrappers. Return 3 and get 1 more chocolate. So total chocolates = 15 + 5 + 1 + 1 Input: money = 20, price = 3, wrap = 5 Output: 7
Nguồn: Câu đố 22 | [Sôcôla tối đa]
Một phương pháp ngây thơ là liên tục đếm số lượng sôcôla bằng cách trả lại giấy gói cho đến khi các giấy gói còn lại didn trở nên ít hơn yêu cầu để có được sô cô la. & NBSP;naive method is to continuously count the number of chocolates by returning wrappers until wrappers left didn’t become less than required to get a chocolate.
Dưới đây là việc thực hiện phương pháp này. & NBSP;
C++
#include
using
namespace
std;
int
countRec[
int
221
int
223
224
225
226
227
228
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]0
225
int
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]3
225
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]6
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]7
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]8
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
int
221
int
223
int
225
int
223
224
225
int
#include
1225
229
#include
4Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
int
#include
7
224
225
int
using
1225
int
using
4225
int
using
7225
using
9225
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]0
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Java
namespace
4 namespace
5
namespace
6 namespace
7
225
namespace
9 int
countRec[
int
221
int
223
225
224
228
226
227
int
1
229
int
3int
4228
int
int
7228
229
countRec[
0countRec[
1countRec[
2
225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
225
namespace
9 int
221
int
2200
countRec[
1int
225
int
223
225
224
228
int
#include
1228
229
#include
4225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
225
2217
namespace
9 2219
2220
225
224
228
int
2225
2226
2227
228
int
2230
2231
2227
228
int
2235
2236
int
4228
2239
int
1
2241
225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Python3
namespace
4
2246
2247
2248
225
226
2251
228
229
int
3int
4225
2257
2258
2259
2260
2261
225
229
2257
2265
2266
2265
2259
2269
2270
2271
2247
2273
225
2259
2258
2277
2260
2279
225
229
2282
2265
2284
2277
2258
2226
int
42289
2258
2231
int
42293
2258
2236
int
42297
2298
C#
using
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]00
namespace
6 namespace
7
225
namespace
9 int
countRec[
int
221
int
223
225
224
228
226
227
int
1
229
int
3int
4228
int
int
7228
229
countRec[
0Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]25
countRec[
2225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
225
namespace
9 int
221
int
2200
countRec[
1int
225
int
223
225
224
228
int
#include
1228
229
#include
4225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
225
2217
namespace
9 2219
2220
225
224
228
int
2225
2226
2227
228
int
2230
2231
2227
228
int
2235
2236
int
4228
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]67
int
1
2241
225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
22
5Find initial number of chocolates by
dividing the amount with per piece cost.
i.e. choc = money / price
then apply below formula
choc += [choc - 1]/[wrap - 1]
9
namespace
4
2246
2247
2248
224
225
226
2251
228
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]0
225
int
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]3
225
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]6
2208
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]78
2210
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
int
221
int
223
int
225
int
223
224
225
int
#include
1225
229
#include
4Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
int
#include
7
225
int
using
1225
int
using
4225
int
using
72257
22
522
9 Find initial number of chocolates by
dividing the amount with per piece cost.
i.e. choc = money / price
then apply below formula
choc += [choc - 1]/[wrap - 1]
0
2258
Java
225
224
228
226
227
int
1
229
int
3int
4228
2271
228
229
countRec[
0countRec[
1countRec[
2
225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]74
2281
countRec[
1
2283
225
224
228
2287
228
229
#include
4225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
2293
2294
228
2296
228
2298
228
#include
00int
1#include
02
#include
03
Đầu ra: & nbsp;
22
Một giải pháp hiệu quả là sử dụng một công thức trực tiếp để tìm số lượng sôcôla. & NBSP;efficient solution is to use a direct formula to find the number of chocolates.
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]
Trong quá trình triển khai ngây thơ ở trên, chúng tôi nhận thấy rằng sau khi tìm thấy số lượng sôcôla ban đầu, chúng tôi đã phân chia số lượng sôcôla cho số lượng gói hàng ban đầu cần thiết. cho đến khi chúng tôi rời đi với 1 sô cô la hoặc trình bao bọc. & nbsp; chúng tôi đang tính toán lại các giá trị, tức là [[Choc/wrap + Choc%wrap]/wrap cho đến khi chúng tôi nhận được 1. & nbsp; của sôcôla và bao bọc cho 1 và sau đó chia chúng để nhận kết quả [Choc-1]/[Wrap-1] & NBSP;
We are recomputing the values i.e. [[choc/wrap + choc%wrap]/wrap until we get 1.
It is observed that we can get the result by just reducing
the values of chocolates and wrappers by 1 and then divide them to get the result [choc-1]/[wrap-1]
Dưới đây là việc thực hiện phương pháp trên:
C++
#include
using
namespace
std;
int
221
int
223
int
225
int
223
224
225
226
#include
19#include
20
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]0
225
int
#include
25225
#include
27225
229
#include
30Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
int
#include
7
224
225
int
using
1225
int
using
4225
int
using
7225
using
9225
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]0
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Java
namespace
4 namespace
5
namespace
6 namespace
7
225
namespace
9 int
221
int
2200
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]25
int
225
int
223
225
224
228
226
#include
19int
1
229
int
3int
4228
int
#include
25228
#include
782231
#include
802231
2210
228
229
#include
30225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
225
2217
namespace
9 2219
2220
225
224
228
int
2225
2226
int
4228
int
2230
2231
int
4228
int
2235
2236
int
4228
2239
using
12
2241
225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Python3
2247
using
18225
226
using
21228
229
int
3225
2259
2258
int
using
292260
using
31225
2259
2258
2259
2265
using
37using
38225
229
int
using
492277
2258
2226
2289
2258
2231
2293
2258
2236
2297
using
60C#
using
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]00
namespace
6 namespace
7
225
namespace
9 int
221
int
2200
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]25
int
225
int
223
225
224
228
226
#include
19int
1
229
int
3int
4228
int
#include
25228
#include
27228
229
#include
30225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
225
2217
namespace
9 2219
2220
225
224
228
int
2225
2226
int
4228
int
2230
2231
int
4228
int
2235
2236
int
4228
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]67
228
2241
225
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
22
47 using
18
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]73
225
226
using
21224
228
229
int
3225
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]0
Java
namespace
4 namespace
5
namespace
6 namespace
7
225
namespace
9 int
221
int
2200
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]25
int
225
int
223
228
226
#include
19int
1
229
int
3int
4228
int
#include
252257
22
822
9 #include
30
2258
225
2217
namespace
9 2219
2220
224
225
226
#include
19#include
20
229
Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]0
225
int
#include
25225
namespace
92namespace
93namespace
94
225
229
#include
30Find initial number of chocolates by dividing the amount with per piece cost. i.e. choc = money / price then apply below formula choc += [choc - 1]/[wrap - 1]9
namespace
99
std;
00
std;
01
std;
02
#include
03
Đầu ra: & nbsp;
22
int
#include
7 O[1] ,as we are not using any looping statements in our program.
22
5int
using
1 O[1] ,as we are not using any extra space.
225
int
using
4Sahil Chhabra [akku]. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.