Forum address: http://topic.csdn.net/u/20081214/00/5285a066-d37b-455f-a3be-4bb15aeda0ef.html? Seed = 290962744
The scenario is: one yuan a bottle of soda, two empty bottles can change one bottle of soda.
If you have X yuan, you can drink y bottles of soda, which is represented by Y = f (x ).
If n empty bottles are used for another empty bottle, y = f (x, n) indicates
My code, if it is 2 for 1:
- Package lihan;
- Public class test {
- Public static integer digui (int x)
- {
- If (x = 1)
- {
- Return X;
- }
- If (X % 2 = 1)
- Return x + digui (X/2) + 1;
- Else
- Return x + digui (X/2 );
- }
- /**
- * @ Param ARGs
- */
- Public static void main (string [] ARGs ){
- // Todo automatically generates method stubs
- Int I = digui (11 );
- System. Out. Print (I );
- }
- }
If n is used for 1, then
- Public class lihan {
- Public static integer digui (int x, int N)
- {
- If (x = 1)
- {
- Return X;
- }
- If (X % N = (n-1 ))
- Return x + digui (x/n, n) + 1;
- Else
- Return x + digui (x/N, N );
- }
- /**
- * @ Param ARGs
- */
- Public static void main (string [] ARGs ){
- // Todo automatically generates method stubs
- Int I = digui (12, 3) + 1;
- System. Out. Print (I );
- }
- }