Hướng dẫn eat the chocolates program in python - ăn sôcôla trong chương trình trăn

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;

  1. Tiền: Tiền bạn phải mua sôcôla
  2. Giá: Giá sô cô la
  3. 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

22
1int
22
3

22
4

22
5
22
6
22
7

22
8
22
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

22
5int
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

22
5
22
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)
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

22
1int
22
3int
22
5int
22
3

22
4

22
5int #include 1

22
5
22
9 #include 4

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 #include 7

22
4

22
5int using1

22
5int using4

22
5int using7

22
5using9

22
5
22
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

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

namespace4 namespace5

namespace6 namespace7

22
5namespace9 int countRec(int
22
1int
22
3

22
5
22
4

22
8
22
6
22
7

int1

22
9 int3int4

22
8int int7

22
8
22
9 countRec(0

countRec(1countRec(2

22
5
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
5namespace9 int
22
1int
22
00

countRec(1int

22
5int
22
3

22
5
22
4

22
8int #include 1

22
8
22
9 #include 4

22
5
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
5
22
17 namespace9
22
19
22
20

22
5
22
4

22
8int
22
25
22
26
22
27

22
8int
22
30
22
31
22
27

22
8int
22
35
22
36 int4

22
8
22
39

int1

22
41

22
5
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

namespace4

22
46

22
47
22
48

22
5
22
6
22
51

22
8
22
9 int3int4

22
5
22
57
22
58
22
59
22
60
22
61

22
5
22
9
22
57
22
65
22
66
22
65
22
59
22
69

22
70
22
71

22
47
22
73

22
5
22
59
22
58
22
77
22
60
22
79

22
5
22
9
22
82
22
65
22
84

22
77
22
58
22
26int4

22
89
22
58
22
31int4

22
93
22
58
22
36 int4

22
97
22
98

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

namespace6 namespace7

22
5namespace9 int countRec(int
22
1int
22
3

22
5
22
4

22
8
22
6
22
7

int1

22
9 int3int4

22
8int int7

22
8
22
9 countRec(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)
25countRec(2

22
5
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
5namespace9 int
22
1int
22
00

countRec(1int

22
5int
22
3

22
5
22
4

22
8int #include 1

22
8
22
9 #include 4

22
5
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
5
22
17 namespace9
22
19
22
20

22
5
22
4

22
8int
22
25
22
26
22
27

22
8int
22
30
22
31
22
27

22
8int
22
35
22
36 int4

22
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)
67

int1

22
41

22
5
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

225Find 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

namespace4

22
46

22
47
22
48

22
4

22
5
22
6
22
51

22
8
22
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

22
5int
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

22
5
22
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)
6

22
08
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
22
10

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

22
1int
22
3int
22
5int
22
3

22
4

22
5int #include 1

22
5
22
9 #include 4

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 #include 7

22
5int using1

22
5int using4

22
5int using7

22
57

225229 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

22
58

Java

22
5
22
4

22
8
22
6
22
7

int1

22
9 int3int4

22
8
22
71

22
8
22
9 countRec(0

countRec(1countRec(2

22
5
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
5
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
22
81

countRec(1

22
83

22
5
22
4

22
8
22
87

22
8
22
9 #include 4

22
5
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
93
22
94

22
8
22
96

22
8
22
98

22
8#include 00

int1#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

22
1int
22
3int
22
5int
22
3

22
4

22
5
22
6 #include 19

#include 20

22
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

22
5int #include 25

22
5#include 27

22
5
22
9 #include 30

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 #include 7

22
4

22
5int using1

22
5int using4

22
5int using7

22
5using9

22
5
22
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

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

namespace4 namespace5

namespace6 namespace7

22
5namespace9 int
22
1int
22
00

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)
25int
22
5int
22
3

22
5
22
4

22
8
22
6 #include 19

int1

22
9 int3int4

22
8int #include 25

22
8#include 78
22
31#include 80
22
31
22
10

22
8
22
9 #include 30

22
5
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
5
22
17 namespace9
22
19
22
20

22
5
22
4

22
8int
22
25
22
26int4

22
8int
22
30
22
31int4

22
8int
22
35
22
36 int4

22
8
22
39

using12

22
41

22
5
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

22
47 using18

22
5
22
6 using21

22
8
22
9 int3

22
5
22
59
22
58 intusing29
22
60 using31

22
5
22
59
22
58
22
59
22
65 using37using38

22
5
22
9 intusing49

22
77
22
58
22
26

22
89
22
58
22
31

22
93
22
58
22
36

22
97using60

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

namespace6 namespace7

22
5namespace9 int
22
1int
22
00

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)
25int
22
5int
22
3

22
5
22
4

22
8
22
6 #include 19

int1

22
9 int3int4

22
8int #include 25

22
8#include 27

22
8
22
9 #include 30

22
5
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
5
22
17 namespace9
22
19
22
20

22
5
22
4

22
8int
22
25
22
26int4

22
8int
22
30
22
31int4

22
8int
22
35
22
36 int4

22
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)
67

22
8
22
41

22
5
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

2247 using18

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

22
5
22
6 using21

22
4

22
8
22
9 int3

22
5
22
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

Java

namespace4 namespace5

namespace6 namespace7

22
5namespace9 int
22
1int
22
00

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)
25int
22
5int
22
3

22
8
22
6 #include 19

int1

22
9 int3int4

22
8int #include 25

22
57

228229 #include 30

22
58

22
5
22
17 namespace9
22
19
22
20

22
4

22
5
22
6 #include 19

#include 20

22
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

22
5int #include 25

22
5namespace92

namespace93namespace94

22
5
22
9 #include 30

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

namespace99

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 using1 O(1) ,as we are not using any extra space.

22
5int using4Sahil 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.