# Lec.0 課堂練習筆記 [TOC] <br> ## 題目編號 a.004 ### 重點一 - EOF的處理與cin.get()的使用 ### 重點二 - %的使用 * 題目連結:[文文的求婚](https://zerojudge.tw/ShowProblem?problemid=a004) :::spoiler 參考程式碼 ```cpp= #include <iostream> #include <cstring> using namespace std; int main(){ int year; while(cin >> year && cin.get() != EOF){ if ((year%4 == 0 && year%100 != 0) || year%400 == 0) { cout << "閏年" << endl; } else { cout << "平年" << endl; } } } ``` ::: <br> ## 題目編號 a.006 ### 重點一 - 連續cin ### 重點二 - sqrt的使用 ### 重點三 - C與C++中的平方計算 ### 重點四 - 程式流程規劃 * 題目連結:[一元二次方程式](https://zerojudge.tw/ShowProblem?problemid=a006) :::spoiler 參考程式碼 ```cpp= #include <iostream> #include <math.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; //先確保根號裡面是正數 //如果根號裡面小於0,就直接印出沒有根,不要繼續計算 if (b*b - 4*a*c < 0){ cout << "No real root"; } //如果根號裡面>=0,算出兩個根,並且比較大小 else { int d1 = ((-b) + sqrt(pow(b, 2) - 4*a*c))/(2*a); int d2 = ((-b) - sqrt(b*b - 4*a*c))/(2*a); //pow(a, b) 回傳a的b次方 //兩個根相同,印出任意一個 if (d1 == d2){ cout << "Two same roots x=" << d1; } //兩個根不同,用<math.h>函式庫裡面的max()和min()來比大小 else { cout << "Two different roots x1=" << max(d1, d2) << " , x2=" << min(d1, d2); } } return 0; } ``` ::: <br> ## 題目編號 a.005 ### 重點一 - 二維陣列的處理 ### 重點二 - 指標的使用 * 題目連結:[Eva的回家作業](https://zerojudge.tw/ShowProblem?problemid=a005) :::spoiler 參考程式碼 - 簡易版 ```cpp= #include <stdio.h> #include <stdlib.h> #include <iostream> #include <math.h> using namespace std; bool isArithmeticSeq(int* Arr){ if ((Arr[0]-Arr[1]) == (Arr[1]-Arr[2])){ return true; } else return false; } int main() { int t; cin >> t; int InputArr[t][5]; for (int i = 0; i < t; i++){ for (int j = 0; j < 4; j++){ cin >> InputArr[i][j]; } } for (int i = 0; i < t; i++){ if(isArithmeticSeq(InputArr[i])){ InputArr[i][4] = InputArr[i][3]*2 - InputArr[i][2]; } else{ InputArr[i][4] = InputArr[i][3]*InputArr[i][3]/InputArr[i][2]; } } for (int i = 0; i < t; i++){ for (int j = 0; j < 5; j++){ cout << InputArr[i][j] << " "; } cout << endl; } return 0; } ``` ::: :::spoiler 參考程式碼 - 完整版 ```cpp= #include <stdio.h> #include <stdlib.h> #include <iostream> #include <math.h> using namespace std; bool isArithmeticSeq(int* Arr, int len){ for (int i = 1; i < len-1; i++){ if ((Arr[i-1]-Arr[i]) != (Arr[i]-Arr[i+1])){ return false; } } return true; } bool isGeometricSeq(int* Arr, int len){ for (int i = 1; i < len-1; i++){ if ((Arr[i-1]/Arr[i]) != (Arr[i]/Arr[i+1])){ return false; } } return true; } int main() { int t; bool flag = false; cin >> t; int InputArr[t][5]; for (int i = 0; i < t; i++){ for (int j = 0; j < 4; j++){ cin >> InputArr[i][j]; } } for (int i = 0; i < t; i++){ if(isArithmeticSeq(InputArr[i], 4)){ InputArr[i][4] = InputArr[i][3]*2 - InputArr[i][2]; flag = true; } else if(isGeometricSeq(InputArr[i], 4)){ InputArr[i][4] = InputArr[i][3]*InputArr[i][3]/InputArr[i][2]; flag = true; } else { cout << "Error input." << endl; } } if (flag){ for (int i = 0; i < t; i++){ for (int j = 0; j < 5; j++){ cout << InputArr[i][j] << " "; } cout << endl; } } return 0; } ``` :::