Created
August 12, 2020 16:23
-
-
Save joelsebbu/9154c25a925f58d6a1b3daa73a3f330e to your computer and use it in GitHub Desktop.
Water Bottles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Given 'n' full water bottles, you can exchange 'f' empty water bottles for one full water bottle. | |
| The operation of drinking a full water bottle turns it into an empty bottle. | |
| Return the maximum number of water bottles you can drink. | |
| Example 1 | |
| Input: n = 9, f = 3 | |
| Output: 13 | |
| Explanation: You can exchange 3 empty bottles to get 1 full water bottle. | |
| Number of water bottles you can drink: 9 + 3 + 1 = 13. | |
| Example 2 | |
| Input: n = 15, f = 4 | |
| Output: 19 | |
| Explanation: You can exchange 4 empty bottles to get 1 full water bottle. | |
| Number of water bottles you can drink: 15 + 3 + 1 = 19. | |
| Example 3 | |
| Input: n = 5, f = 5 | |
| Output: 6 | |
| Example 4: | |
| Input: n = 2, f = 3 | |
| Output: 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| int solve(int n,int f){ | |
| int length=n,ans=n; | |
| if(f>n) return n; | |
| else while(length>=f){ | |
| ans+=length/f; | |
| length=(length/f)+(length%f); | |
| } | |
| return ans; | |
| } | |
| int main() | |
| { | |
| int n,f; | |
| cin>>n>>f; | |
| cout<<"ans ="<<solve(n,f); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment