日期:2025年3月8日
ZeroJudge 題目連結:f375. 神奇肥料 Fertilizer
解題想法
按照題目指定規則寫條件即可。注意:每天都可以檢查花的高度,即使是每 9 天的休假日,如果花的高度大於等於顧客要求的高度,仍然可以印出天數。
Python 程式碼
使用時間約為 39 ms,記憶體約為 3.3 MB,通過測試。
import sys
for line in sys.stdin:
s, e, a = map(int, line.split()) # 起始高度 s、目標高度 e、耐性 a
h, d = 0, 0 # 增加的高度,天數
while True:
s += h; d += 1 # 更新 s 及 d
if d%11 == 0 and s < e: # 每隔 11 天而且 s 小於 e
a -= 1 # a 減 1
if a == 0: # 如果 a 等於 0
print("unsalable"); break # 印出 unsalable 並中止迴圈
if s >= e: # 如果 s 大於等於 e
print(d); break # 印出天數並中止迴圈
else: # 如果 s 小於 e
if d%9 == 0 or d%10 == 0: h = 0 # 每隔 9 天或 10 天,h 為 0
elif d%3 == 0: h = s//3 # 每隔 3 天且不是休假日,h 為 s//3
else: h = s//10 # 其它天,h 為 s//10
C++ 程式碼
使用時間約為 2 ms,記憶體約為 344 kB,通過測試。
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int s, e, a; // 起始高度 s、目標高度 e、耐性 a
while(cin >> s >> e >> a) {
int h = 0, d = 0; // 增加的高度,天數
while(true) {
s += h; d++; // 更新 s 及 d
if (d%11 == 0 && s < e) { // 每隔 11 天而且 s 小於 e
a--; // a 減 1
if (a == 0) { // 如果 a 等於 0
cout << "unsalable\n"; // 印出 unsalable 並中止迴圈
break;
}
}
if (s >= e) { // 如果 s 大於等於 e
cout << d << "\n"; // 印出天數並中止迴圈
break;
} else { // 如果 s 小於 e
if (d%9 == 0 || d%10 == 0) h = 0; // 每隔 9 天或 10 天,h 為 0
else if (d%3 == 0) h = s/3; // 每隔 3 天且不是休假日,h 為 s/3
else h = s/10; // 其它天,h 為 s/10
}
}
}
return 0;
}
沒有留言:
張貼留言