日期:2025年2月16日
ZeroJudge 題目連結:e969. 大吃大喝 (Big eater)
解題想法
這題要很注意輸出的內容,很容易弄錯一些細節。
Python 程式碼
使用時間約為 20 ms,記憶體約為 3.3 MB,通過測試。
n, m, k = map(int, input().split())
t = 0
food = ("eats an Apple pie", "drinks a Corn soup")
imin = (32, 55)
if n < imin[k]:
print("Wayne can't eat and drink.")
else:
while n >= imin[k]:
n -= imin[k]
print(f"{t:d}: Wayne {food[k]:s}, and now he ", end="")
if n == 0: print(f"doesn't have money.")
elif n == 1: print(f"has 1 dollar.")
else: print(f"has {n:d} dollars.")
k = (k+1)%2
t += m
C++ 程式碼
使用時間約為 7 ms,記憶體約為 336 kB,通過測試。
#include <iostream>
#include <string>
using namespace std;
int main() {
int n, m, k; cin >> n >> m >> k;
int t = 0;
string food[2] = {"eats an Apple pie", "drinks a Corn soup"};
int imin[2] = {32, 55};
if (n < imin[k]) {
cout << "Wayne can't eat and drink.\n";
} else {
while(n >= imin[k]) {
n -= imin[k];
cout << t << ": Wayne " << food[k] << ", and now he ";
if (n == 0) cout << "doesn't have money.\n";
else if (n == 1) cout << "has 1 dollar.\n";
else cout << "has " << n << " dollars.\n";
k = (k+1)%2;
t += m;
}
}
return 0;
}
沒有留言:
張貼留言