Weekly Contest
限制 :
1 <= x <= 100
這題是要把字元拆開再組回去,然後看有沒有跟原數有倍數關係。
程式碼:
class Solution { public: int sumOfTheDigitsOfHarshadNumber(int x) { if(x == 100) return 1; int num = x%10 + x/10; if(x % num) return -1; return num; } };
1 <= numBottles <= 100
1 <= numExchange <= 100
這題其實就是依照喝、交換、換回來的瓶子做運算,跟著他給的步驟就好。
class Solution { public: int maxBottlesDrunk(int numBottles, int numExchange) { int bottlesDrink = 0; int fullBottles = numBottles; numBottles = 0; while (fullBottles > 0) { // drink bottlesDrink += fullBottles; numBottles += fullBottles; fullBottles = 0; // exchange while (numBottles >= numExchange) { fullBottles += 1; numBottles -= numExchange++; } } return bottlesDrink; } };
104
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up