# A0001-教師示範程式 * 字串要使用string宣告喔! * cin和cout的箭頭方向要小心! * 檢查每一行最後有沒有分號,記得是「半形分號」喔! * 題目的範例輸出格式請看清楚,有空格喔! * 輸出的字串記得用"想要輸出的文字"。 ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; cout << "向 " << s << " 致敬"; return 0; } ``` --- # A0002-第一個線上程式 ## 小提示 * cin和cout的箭頭方向要小心! * 檢查每一行最後有沒有分號,記得是「半形分號」喔! ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; cout << n*n*n*n*n; return 0; } ``` ## 高手過招 * 如果會的話,可以用pow(n,5)來讓程式更整潔。 --- # A0006-分數的等第 * cin和cout的箭頭方向要小心! * 檢查每一行最後有沒有分號,記得是「半形分號」喔! * 兩個條件同時成立用and,兩個條件其中一個成立用or。(也可以用&&和||) * 輸出的字串記得用"想要輸出的文字"。 ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; if(n>100 or n<0){ cout << "錯誤"; } else if(n>=90 and n<=100){ cout << "優"; } else if(n>=80 and n<90){ cout << "甲"; } else if(n>=70 and n<80){ cout << "乙"; } else if(n>=60 and n<70){ cout << "丙"; } else if(n>=0 and n<60){ cout << "丁"; } return 0; } ``` ## 想一想 1. 可以把最後一個else if改成else嗎? --- # A0012-質數判斷 * cin和cout的箭頭方向要小心! * 檢查每一行最後有沒有分號,記得是「半形分號」喔! * 相等是雙等號。 ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; bool a=true; for(int i=2;i<=n/2;i++){ if(n%i == 0){ a=false; } } if(a){ cout << "質數"; } else{ cout << "合數"; } return 0; } ``` ## 高手過招 * 如果會break的話,其實在偵測到有因數的時候,就可以用break提早結束迴圈了。 ## 想一想 1. 為什麼迴圈是i<=n/2,而不是i<=n呢? --- # A0008-兩數互質嗎? * cin和cout的箭頭方向要小心! * 檢查每一行最後有沒有分號,記得是「半形分號」喔! * 不等於用!=表示。 * 這題使用的算法是輾轉相除法,不會的不妨GOOGLE一下,其實只是國小數學喔! ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ int a, b, temp; cin >> a >> b; while(b != 0){ temp = b; b = a % b; a = temp; } if(a==1){ cout << "True"; } else{ cout << "False"; } } ``` --- # A0014-算術基本定理 * cin和cout的箭頭方向要小心! * 檢查每一行最後有沒有分號,記得是「半形分號」喔! * 相等是雙等號。 * 不等於用!=表示。 * break會打斷「最近的」迴圈。 * now++其實是now=now+1的縮寫。 ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; int now=2; while(n!=1){ while(n%now == 0){ cout << now; n=n/now; if(n==1){ break; } else{ cout << "*"; } } now++; } return 0; } ``` ## 想一想 1. 還有什麼東西像now++一樣可以縮寫的嗎? # o076. 1. 特技表演 ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int a[n]; for(int i=0;i<n;i++){ cin >> a[i]; // 讀取每棟樓的高度到陣列中 } int far=1; // 當前長度 int large=1; // 最長長度 for(int i=1;i<n;i++){ // 判斷這一棟樓和前一棟樓的高度(決定是否能滑翔過去) if(a[i-1]>a[i]){ far++; } // 將此段長度和最大長度比較 並且重設長度 else{ if(far>large){ large=far; far=1; } else{ far=1; } } } // 最後再判斷一次(有可能因為最後一段沒有被阻擋,導致沒有進去else中) if(far>large){ large=far; } cout << large; return 0; } ``` # o077. 2. 電子畫布 ```cpp=1 #include <bits/stdc++.h> using namespace std ; int main () { int h,w,n; cin >> h >> w >> n; int area[h][w]; for (int i = 0 ; i < h ; i++){ for (int j = 0 ; j < w ; j++){ area[i][j] = 0; } } int r,c,t,x; for ( int k = 0 ; k < n ; k++){ cin >> r >> c >> t >> x ; for (int i = 0 ; i < h ; i++){ for (int j = 0 ; j < w ; j++){ if( abs(i-r) + abs(j-c) <= t){ area[i][j] += x ; } } } } for (int i = 0 ; i < h ; i++){ for ( int j = 0 ; j < w ; j++){ cout << area[i][j] << " "; } cout << endl; } return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up