# TOI 2020 新手組程式碼放置區 ## 2020/10 ### 1. 週年慶 [zj題號:f373](https://zerojudge.tw/ShowProblem?problemid=f373) :::spoiler 程式碼1 ```cpp= #include <iostream> using namespace std; int main() { int p; cin >> p; if((p/1000)%2) cout << p-p/1000*100 << " 1\n"; else cout << p-p/2000*200 << " 0\n"; return 0; } ``` ::: :::spoiler ```cpp= ``` ::: ### 3.神奇肥料 [zj題號:f375](https://zerojudge.tw/ShowProblem?problemid=f375) #### 事前:解題分析 - 總共有幾種情況?special case? - 用if-else if-else判讀的順序為? - 同樣用除法,何時取商,何時取餘數? - 迴圈主體是用高度計數,還是天數計數? - 跳出迴圈的條件? #### 事中:程式語法選擇 - 用for-loop還是while? - 變數起始值?終止條件不等式? - 判斷條件、天數/高度計數、耐心值遞減的先後順序? - 輸出是否需要+-1的調整? - 想要用什麼計數方式? #### 事後:想錯誤,生測資 1. 遇到這些狀況,問題可能出在哪?如何測試? ![](https://i.imgur.com/tMn7l6L.png) ![](https://i.imgur.com/H27bDX1.png) 2. 範例測資中,少了哪些情況? 小心!不是過了範測,程式碼就一定是對的! :::spoiler 程式碼0(詳解C++版,再改成while) ```cpp= #include <iostream> using namespace std; int main() { int s, e, a; cin >> s >> e >> a; int d = 1; while(s <= e){ if(s == e){ cout << "1\n"; break; } //進到下一天之前,看還顧客有沒有耐性 if(d%10 == 1 && d!=1) a--; if(a == 0 && s<e){//s<e其實不需要 cout << "Unsalable\n"; break; } //在第d天計算第d+1天會有的量 if(d%3 == 0 && d%10 != 9){ s += s/3; }else if(d%10 != 9 && d%10 != 0){ s += s/10; } //預先檢查d+1天有沒有成功 if(s >= e){ cout << d+1 << "\n"; break; } //第d+1天來了 d++; } return 0; } ``` ::: :::spoiler 程式碼1 ```cpp= #include <iostream> using namespace std; int main() { int s, e, a; cin >> s >> e >> a; int i, h = s; // count by day: 第i天就算這天該有的量 for(i = 1; h<e && a>0; i++){ if (i % 10 == 0 || i % 10 == 1){ ; }else if(i%3 == 1){// (3的倍數)天撒肥(除休假日外),(3的倍數+1)天算法有變 h += h/3; }else{// 其他狀況都是澆水 h += h/10; } if(i>10 && i % 10 == 1) a--; //cout << "day " << i << ",\th = " << h << ",\ta = " << a << endl; } if(h<e || a==0) cout << "unsalable\n"; else if(i == 1){ cout << i << endl; }//i++的時候會多一天,要特別小心 else cout << i-1 << endl; return 0; } ``` ::: :::spoiler 程式碼2 ```cpp= #include <iostream> using namespace std; int main() { int s, e, a; cin >> s >> e >> a; int i, h = s; // count by day: 算完隔天的量,再進入隔天(i++天) for(i = 1; h<e && a>0; i++){ // 先排除休假日 if (i % 10 == 9 || i % 10 == 0){ ; }else if(i%3 == 0){// (3的倍數)天撒肥 h += h/3; }else{// 其他天都是澆水 h += h/10; } if(i>10 && i % 10 == 1) a--; //cout << "day " << i+1 << ",\th = " << h << ",\ta = " << a << endl; } if(h<e || a==0) cout << "unsalable\n"; else cout << i << endl; return 0; } ``` :::