//方法一 int money = 20; int pingzi = 0; int total = 0; while (money > 0 || pingzi > 1) { total++; if (money > 0) { money--; pingzi++; } else if (pingzi > 1) { pingzi--; } } Console.WriteLine(total.ToString());
//方法二 int money = 20; int bottle = money; int sum = bottle; while (bottle > 1) { sum += bottle / 2; bottle = bottle / 2 + bottle%2; } Console.WriteLine(sum.ToString()); //方法三 迭代 int a = drink(20); Console.WriteLine(a);
private static int drink(int n) { if (n == 1) return 1; //如果只有一瓶水了,不可以借瓶子,就只能喝这一瓶 return 1,如果可以借,就喝两瓶,return 2; return 2 + drink(n - 1); }