[C++] NCYU 課堂練習05
請不要直接抄寫程式碼! 也請不要直接跳到程式碼部分,除非你已經寫好了,想要參考別人的寫法! 第一題 邏輯判斷!按照題目敘述邏輯 #include<iostream> using namespace std; int main() { int y; cin>>y; if( ( y%4==0 && y%100!=0 ) || y%400==0 ) cout<<"YES"; else cout<<"NO"; } 第二題 輸出會有七種可能,慢慢討論 可以分成四種象限與其他部分 #include <iostream> using namespace std; int main() { int x,y; cin>>x>>y; if ( x!=0 && y!=0 ){ if ( x>0 ){ if ( y>0 ) cout<<"第一象限"; else cout<<"第四象限"; } else{ if ( y>0 ) cout<<"第二象限"; else cout<<"第三象限"; } } else{ if ( x==0 && y==0 ) cout<<"原點"; else if ( x==0 ) cout<<"Y軸"; else cout<<"X軸"; } } 第三題 可利用題目的年分來當作起使判斷(2019為己亥,以此類推) 也可用定義解(上網查) 天干地支分開判斷,case 數會比較少 #include <iostream> using namespace std; int main() { int year; cin>>year; int x = year%10, y = year%12; switch(x){ case 4: cout<<"甲"; break; case 5: cout<<"乙"; break; case 6: cout<<"丙"; break; case 7: cout<<"丁"; break; case 8: cout<<"戊"; break; case 9: cout<<"己"; break; case 0: cout<<"庚"; break; case 1: cout<<"辛"; break; case 2: cout<<"壬"; break; case 3: cout<<"癸"; break; } switch(y){ case 4: cout<<"子"; break; case 5: cout<<"丑"; break; case 6: cout<<"寅"; break; case 7: cout<<"卯"; break; case 8: cout<<"辰"; break; case 9: cout<<"巳"; break; case 10: cout<<"午"; break; case 11: cout<<"未"; break; case 0: cout<<"申"; break; case 1: cout<<"酉"; break; case 2: cout<<"戌"; break; case 3: cout<<"亥"; break; } } 第四題 每個 char (字元)都會有一個值,稱為 ASCII 利用 ASCII 來確認該字是否在 小寫區間 #include <iostream> using namespace std; int main() { char word; cin>>word; // a 97 z 122 A 65 Z 90 if ( 97<=word && word<=122 ) cout<<"Character "<<word<<" is a lowercase letter....