# <font size=9>**C++考古題**</font> # <font size=3 color=darkblue>**10601:**</font> ## 第一題 輸入列數後,輸出相對的圖形。 ### 第一解: ```cpp= #include <iostream> using namespace std; int main() { int n; while(cin >> n){ for(int i=0;i<n;i++){ for(int j=0;j<=i;j++){ cout << "*"; } cout << endl; } } return 0; } ``` **input** ``` 6 ``` **output** ``` * ** *** **** ***** ****** ``` <Br> ### 第二解: ```cpp= #include <iostream> using namespace std; int main() { int n; while(cin >> n){ for(int i=n;i>0;i--){ for(int j=0;j<i;j++){ cout << "*"; } cout << endl; } } return 0; } ``` **input** ``` 6 ``` **output** ``` ****** ***** **** *** ** * ``` <Br> ### 第三解: ```cpp= #include <iostream> using namespace std; int main() { int n; while(cin >> n){ for(int i=1;i<=n;i++){ for(int j=0;j<n-i;j++)cout<<" "; //印空格 for(int k=0;k<i;k++)cout<<"*"; //印星號 cout<<endl; } } return 0; } ``` **input** ``` 6 ``` **output** ``` * ** *** **** ***** ****** ``` <Br> ### 第四解: ```cpp= #include <iostream> using namespace std; int main() { int n; while(cin >> n){ for(int i=1;i<=n;i++){ for(int k=1;k<i;k++)cout<<" "; //印空格 for(int j=n-i;j>=0;j--)cout<<"*"; //印星號 cout<<endl; } } return 0; } ``` **input** ``` 6 ``` **output** ``` ****** ***** **** *** ** * ``` <Br> ## 第二題 假設某月份為 31 天,輸入該月份 1 日的星期數,輸出該月分之月曆。 ```cpp= #include <iostream> #include <cstdio> using namespace std; int main() { int weekday; while(cin >> weekday){ for(int i=1;i<32;i++){ printf("某月%d日 星期%d\n",i,weekday); //cout << "某月"<<i<<"日 星期"<<weekday<<endl; if(weekday==7)weekday=1; else weekday++; } } return 0; } ``` ## 第三題 輸入正整數 n,輸出如下數列,直到數字超過 n 為止。 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 …… ```cpp= #include <iostream> using namespace std; int main() { int n; while(cin>>n){ for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ cout<<i<<","; } } cout<<"\b \n";//游標退一格用空格把逗號吃掉再換行 } return 0; } ``` ## 第四題 輸入正整數 n,輸出 n*n 乘法表。 ```cpp= #include <iostream> #include <cstdio> using namespace std; int main() { int n; while(cin>>n){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ printf("%d*%d=%d ",i,j,i*j); } cout<<"\n"; } } return 0; } ``` ## 第五題 輸入西元年、月、日,輸出此日期是該年的第幾天?。 * **西元年份除以4不可整除,為平年。** **西元年份除以4可整除,且除以100不可整除,為閏年。** **西元年份除以100可整除,且除以400不可整除,為平年** **西元年份除以400可整除,為閏年。** ### 基本解法: ```cpp= #include <iostream> using namespace std; int leap_year(int); int year_day(int,int,bool); int main() { int year,month,day; while(cin >> year >> month >> day){ bool yes=leap_year(year); //紀錄leap_year回傳值 day=year_day(month,day,yes); //呼叫year_day來計算天數 並把計算結果回傳給day cout<<day<<endl; } return 0; } int leap_year(int y){ //判斷閏年 如果是 回傳1 不是 回傳0 if(y%400==0)return 1; else if(y%4==0&&y%100!=0)return 1; else return 0; } int year_day(int m,int d,bool YoN){ //計算天數 if(YoN==0){ //平年天數計算 for(int i=1;i<m;i++){ switch(i){ case 2: d+=28; // d+=28的意思是 d=d+28 break; case 1: case 3: case 5: case 7: case 8: case 10: d+=31; break; default: d+=30; break; } } }else{ //閏年天數計算 for(int i=1;i<m;i++){ switch(i){ case 2: d+=29; break; case 1: case 3: case 5: case 7: case 8: case 10: d+=31; break; default: d+=30; break; } } } return d; } ``` ### 較短寫法: ```cpp= #include <iostream> using namespace std; int leap_year(int); int year_day(int,int,int); int main() { int year,month,day; while(cin >> year >> month >> day){ int yes=leap_year(year); //紀錄leap_year回傳值 day=year_day(month,day,yes); //呼叫year_day來計算天數 並把計算結果回傳給day cout<<day<<endl; } return 0; } int leap_year(int y){ //判斷閏年 也就是判斷2月為幾天 如果是閏年 回傳29 不是 回傳28 if(y%400==0 || y%4==0 && y%100!=0)return 29; else return 28; } int year_day(int m,int d,int YoN){ //計算天數 for(int i=1;i<m;i++){ switch(i){ case 2: //YoN = 主函式的yes變數 =判斷完是否為閏年的2月天數 d+=YoN; break; case 1: //大月 case 3: case 5: case 7: case 8: case 10: d+=31; break; default: //小月 d+=30; break; } } return d; } ``` ## 第六題 請輸入一個字串(最多 10 個字元),將字串中的文字大小寫互換後輸出。 ```cpp= #include <iostream> #include <cstdio> #include <string> using namespace std; int main() { char c; string ENG; while(cin >> ENG && sizeof(ENG)<=10){ //限制最多只能輸入10個字母 for(int i=0;i<=sizeof(ENG)+1;i++){ c=ENG[i]; //將字串內的字母拿出來單個看 if(c>=65&&c<=90)printf("%c",tolower(c)); //大寫轉小寫 else if(c>=97&&c<=122)printf("%c",toupper(c)); //小寫轉大寫 } cout<<endl; } return 0; } ``` ## 第七題 輸入一些正整數(-1 代表結束),輸出這些整數中的最大值與最小值。 ```cpp= #include <iostream> using namespace std; int main() { int num,max,min; while(cin>>num){ //可以重複做 因為我也不知道祖鳳要的是啥 如果段考考這題這行就先不要寫 max=num;min=num; while(cin>>num&&num!=-1){ //重複做直到-1輸出最大最小值 if(max<num)max=num; if(min>num)min=num; } cout<<"max="<<max<<",min="<<min<<endl; } return 0; } ``` ## 第八題 輸入一些符號(a~z,0 代表結束),輸出各種符號出現的次數。 ```cpp= #include <iostream> using namespace std; int main() { char AtoZ; int ENG[26]; //紀錄字母有幾個的陣列 for(int i=0;i<26;i++)ENG[i]=0; //陣列規0 while(cin >> AtoZ && AtoZ!='0'){ for(int i=0;i<26;i++){ if(AtoZ==char(i+97))ENG[i]++; //判斷輸入的字元是誰 並記錄出現幾次 } } for(int i=0;i<26;i++)cout << char(i+97) << "有" << ENG[i] << "個" << endl; return 0; } ``` ## 第九題 輸入 n 個正整數(n<=10,-1 代表結束),以氣泡排序法輸出由小排到大的結果。 * [氣泡排序法教學](https://medium.com/@oturngo/study-note-01-%E6%B0%A3%E6%B3%A1%E6%8E%92%E5%BA%8F%E6%B3%95-bubble-sort-ee534b6f91eb) ```cpp= #include <iostream> using namespace std; int main() { int n[10]; for(int i=0;i<10;i++)n[i]=-1; //一開始全設-1待會比較方便 while(1){ //重複執行 for(int i=0;i<10;i++){ //輸入直到超過十個或輸入到-1 cin>>n[i]; if(n[i]==-1)break; } for(int i=0;i<10;i++){ //氣泡排序法 if前面的數比後一個數大就交換 for(int j=0;j<10;j++){ if(n[j]>n[j+1]){ int temp=n[j]; n[j]=n[j+1]; n[j+1]=temp; } } } for(int i=0;i<10;i++){ //輸出 if遇到-1就跳過 if(n[i]==-1)continue; cout<<n[i]<<" "; } cout<<endl; for(int i=0;i<10;i++)n[i]=-1; //將陣列全設為-1 } return 0; } ``` ## 第十題 假設西元 2000 年 1 月 1 日為星期六。分三列輸入某年某月某日,輸出該日期是星期幾。 <font color=red>這題我用暴力解,所以如果不會沒關西</font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> using namespace std; void low_year(int,int,int); void high_year(int,int,int); int main() { int year,month,day; while(cin>>year>>month>>day){ if(year>=2000){ high_year(year,month,day); }else{ low_year(year,month,day); } } return 0; } void low_year(int y,int m,int d){ int distance_day=0; for(int i=y;i<2000;i++){ if(i%400==0 || i%4==0 && i%100!=0)distance_day+=366; else distance_day+=365; } for(int i=1;i<m;i++){ switch(i){ case 4: case 6: case 9: case 11: d+=30; break; case 2: d+=28; break; default: d+=31; break; } } distance_day-=d; //到這邊是在計算距離1999/12/31有幾天 switch(distance_day%7){ //這邊我就暴力解 一個一個找條件 像是1999/12/31是星期五 然後她的餘數是0 所以0就是星期五 case 6: cout<<"星期六"<<endl; break; case 5: cout<<"星期日"<<endl; break; case 4: cout<<"星期一"<<endl; break; case 3: cout<<"星期二"<<endl; break; case 2: cout<<"星期三"<<endl; break; case 1: cout<<"星期四"<<endl; break; default: cout<<"星期五"<<endl; break; } } void high_year(int y,int m,int d){ for(int i=2000;i<y;i++){ if(i%400==0||i%4==0&&i%100!=0)d+=366; else d+=365; } for(int i=1;i<m;i++){ switch(i){ case 4: case 6: case 9: case 11: d+=30; break; case 2: d+=28; break; default: d+=31; break; } } if(m>2&&y%400==0||y%4==0&&y%100!=0)d++; //到這邊計算有幾天 switch(d%7){ //暴力解 case 2: cout<<"星期日"<<endl; break; case 3: cout<<"星期一"<<endl; break; case 4: cout<<"星期二"<<endl; break; case 5: cout<<"星期三"<<endl; break; case 6: cout<<"星期四"<<endl; break; case 0: cout<<"星期五"<<endl; break; default: cout<<"星期六"<<endl; break; } return; } ``` ::: <br> ## 可樂題(CPE考題 祖鳳好狠) 簡單說就是你有n瓶可樂,可以跟老闆用三瓶空瓶換一瓶全新可樂,且可以跟老闆借瓶子,你總共可以喝的最大瓶數為幾瓶(我應該會先撐死) <font size=3 color=darkpink>**以下是隔壁阿平速解**</font> ```cpp= #include<iostream> using namespace std; int main(){ int n; while(cin >> n){ cout << n+n/2 << endl; } return 0; } ``` # <font size=3 color=darkblue>**11001:**</font> ## 第四題 輸入一個正奇數,若非正奇數,須繼續輸入。輸出 1 到此數的奇數和。 ```cpp= #include <iostream> using namespace std; int main() { int n; while(cin >> n){ if(n%2==0)continue; int sum=0; for(int i=1;i<=n;i+=2){ sum+=i; } cout << sum << endl; } return 0; } ``` ## 第五題 輸入一個十進位正整數,輸出相對應之二進位數字。 **如果想知道自己有沒有寫錯,可以點下面網址去試試,記得登入google帳號,登入才能送出解答** [點我](https://zerojudge.tw/ShowProblem?problemid=a034) ```cpp= #include <iostream> using namespace std; int main() { int n,c=0; int num[10000]={0}; while(cin >> n && n>-1){ for(int i=n;i>0;i/=2){ //紀錄二進位 num[c]=i%2; c++; //紀錄二進位位置 } for(int i=c-1;i>=0;i--){ cout<<num[i]; } cout<<endl; c=0; } return 0; } ``` ## 第七題 假設西元 2021 年 1 月 1 日為星期五。輸入 2021 年某月某日, 輸出 1. 該日期為星期幾 1. 該月份之月曆 <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include<iostream> using namespace std; void whatday(int,int); void monthly_calendar(int); int main() { int month,day; while(cin >> month >> day){ whatday(month,day); monthly_calendar(month); } return 0; } void whatday(int m,int d){ //第一小題 int day=d; for(int i=1;i<m;i++){ switch (i){ case 2: d+=28; break; case 4: case 6: case 9: case 11: d+=30; break; default: d+=31; break; } } switch (d%7){ case 1: printf("2021/%d/%d為星期五\n",m,day); break; case 2: printf("2021/%d/%d為星期六\n",m,day); break; case 3: printf("2021/%d/%d為星期日\n",m,day); break; case 4: printf("2021/%d/%d為星期一\n",m,day); break; case 5: printf("2021/%d/%d為星期二\n",m,day); break; case 6: printf("2021/%d/%d為星期三\n",m,day); break; default: printf("2021/%d/%d為星期四\n",m,day); break; } return; } void monthly_calendar(int m){ //印月曆 int d=1; for(int i=1;i<m;i++){ switch (i){ case 2: d+=28; break; case 4: case 6: case 9: case 11: d+=30; break; default: d+=31; break; } } cout << m << "月" << "\n"<< " 一 二 三 四 五 六 日" << endl; int whereday=0; switch (d%7){ case 1: cout << " 1"; whereday=5; break; case 2: cout << " 1"; whereday=6; break; case 3: cout <<" 1"; whereday=7; break; case 4: cout <<" 1"; whereday=1; break; case 5: cout <<" 1"; whereday=2; break; case 6: cout << " 1"; whereday=3; break; default: cout <<" 1"; whereday=4; break; } for(int i=2;i<32;i++){ if(m==2 && i==29)break; if(i==31)if(m==3||m==5||m==9||m==11)break; if(whereday==7){ cout<<endl; whereday=0; } if(i>=10){ cout << " " << i; }else{ cout <<" " << i; } whereday++; } cout <<endl; return; } ``` ::: <br> ## 第八題 請先寫一個函數判斷整數是否為質數,函數中不能有輸入或輸出。並於主程式 輸入正整數 n,藉由呼叫此函數,輸出大於 1 且小於 n 的質數。 ```cpp= #include<iostream> using namespace std; bool prime(int); int main(){ int n; while (cin >> n){ for(int i=2;i<=n;i++){ bool YoN=prime(i); //i這個數是否為質數 "是" 為 "1" "否" 為 "0" if(YoN==1)cout << i << " "; } cout << endl; } return 0; } bool prime(int n){ //n是否為質數 在寫程式習慣將 yes 為 1 no 為 0 for(int i=2;i<=n/2;i++){ if(n%i==0)return 0; //有被數字整除 回傳0 此時因為已經回傳值了 所以不論for迴圈有沒有跑完 皆會直接返回主函式 } //注意! 如果for迴圈跑完都沒吻合if的條件 才會跑到21行 return 1; //沒被任何數整除 回傳1 } ``` ## 第九題 請寫一函數(function)計算整數次方,當傳入整數 $x$ ,則回傳整數 $x^x$ 。於主程式輸入整數 n(n<=10),藉呼叫此函數,輸出 $1^1+2^2+3^3+...n^n$ 的總和。 ```cpp= #include<iostream> using namespace std; int sum(int); int main(){ int n,ans; //注意! 這邊的n 跟 sum 的 n 是不一樣的變數 while(cin >> n && n<=10){ ans=0; for(int i=1;i<=n;i++){ ans=ans+sum(i); } cout<<ans<<endl; } } int sum(int n){ //做n的n次方 int ans=1; for(int i=1;i<=n;i++){ ans*=n; //如果n的值為2 這個for迴圈會做2次ans乘2的動作 } //也就是當for迴圈跑完時 ans為2的2次方 return ans; } ``` ## 第十題 輸入兩個正整數,判斷兩數相加過程中會發生幾次進位? ```cpp= #include<iostream> using namespace std; int main(){ int n1,n2,ans=0; while (cin >> n1 >> n2){ int sum=0; while(n1>0||n2>0){ sum=n1%10+n2%10+sum; if(sum>=10){ ans++; sum=1; } n1/=10; n2/=10; } cout << "ans=" << ans << endl; ans=0; } } ``` <br> # <font size=3 color=darkblue>**10701:**</font> ## 第四題 輸入正整數n,輸出各點數出現次數跟機率。 ```cpp= #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int n; cout << "請輸入一個正整數:"; while(cin >> n){ srand(time(NULL)); int dice[6]={0}; for(int i=0;i<n;i++){ dice[rand()%6]++; } for(int i=0;i<6;i++){ cout << i+1 << "點 : " << dice[i] << "次,機率為 : " << dice[i]*100/n << "%\n"; } cout << "請輸入一個正整數:"; } return 0; } ``` ## 第五題 以<font color=red>**字串**</font>方式輸入兩個正整數(二位數)的運算式,輸出其答案。 **string解:** ```cpp= #include <iostream> #include <string> using namespace std; int main() { string r; cout << "請輸入運算式:"; while(cin >> r){ int n1,n2; n1=(r[0]-48)*10+(r[1]-48); n2=(r[3]-48)*10+(r[4]-48); cout << n1 << r[2] << n2 << " = "; if(r[2]=='+')cout << n1+n2 << "\n請輸入運算式:"; else if(r[2]=='-')cout << n1-n2 << "\n請輸入運算式:"; else if(r[2]=='*')cout << n1*n2 << "\n請輸入運算式:"; else if(r[2]=='/')cout << n1/n2 << "\n請輸入運算式:"; } return 0; } ``` **字元陣列解:** ```cpp= #include <iostream> using namespace std; int main() { char r[5]; cout << "請輸入運算式:"; while(cin >> r){ int n1,n2; n1=(r[0]-48)*10+(r[1]-48); n2=(r[3]-48)*10+(r[4]-48); cout << n1 << r[2] << n2 << " = "; if(r[2]=='+')cout << n1+n2 << "\n請輸入運算式:"; else if(r[2]=='-')cout << n1-n2 << "\n請輸入運算式:"; else if(r[2]=='*')cout << n1*n2 << "\n請輸入運算式:"; else if(r[2]=='/')cout << n1/n2 << "\n請輸入運算式:"; } return 0; } ``` ## 第八題 以<font color=red>**字串**</font>輸入一些0~9的數,長度小於20,輸出各種數字出現的次數和總和,0次的勿輸出。 ```cpp= #include <iostream> #include <string> using namespace std; int main() { string NUM; cout << "請輸入一些數字:"; while(cin >> NUM){ int num[10]={0},sum=0; for(int i=0;i<NUM.size()&&i<20;i++){ num[NUM[i]-48]++; } for(int i=0;i<10;i++){ if(num[i]>0){ cout << i << "有 " << num[i] << " 個\n"; sum=num[i]*i+sum; } } cout << "總和為 : " << sum << "\n請輸入一些數字:"; } return 0; } ``` ## 第十題 模擬橋牌發牌,輸出四位玩家所得13張牌,包含花色及點數(A=4 , K=3 , Q=2 , J=1 , 其它=0)。 我盡量打詳細點QQ :::spoiler **陣列看不懂點這** <br> card陣列大概長這樣,每個花色的牌各有一張 左上SA在陣列表示card[0][0],看成座標就是(0,0)的位置,右上為(0,12)代表SK,在陣列表示card[0][12] <br> | | A |2 |3 | 4 | 5 |6 | 7 | 8 |9 | 10 | J |Q | K | | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | | S | 1 | 1 |1|1| 1 |1|1| 1 |1|1| 1 |1|1| |H| 1 | 1 |1|1| 1 |1|1| 1 |1|1| 1 |1|1| |D| 1 | 1 |1|1| 1 |1|1| 1 |1|1| 1 |1|1| |C| 1 | 1 |1|1| 1 |1|1| 1 |1|1| 1 |1|1| ▲ card尚未被抽卡前 <br> 當我從card也就是牌組中抽取一張牌,那該張牌在牌組所對應的位置就必須為0,並且將這張牌丟入玩家的牌組中,也就是將player_card這個陣列相對應的位置改為1,代表此張牌在玩家牌組中 <br> | | A |2 |3 | 4 | 5 |6 | 7 | 8 |9 | 10 | J |Q | K | | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | |S|0|0|0|0|0|0|0|0|0|0|0|0|0| |H|0|0|0|0|0|0|0|0|0|0|0|0|0| |D|0|0|0|0|0|0|0|0|0|0|0|0|0| |C|0|0|0|0|0|0|0|0|0|0|0|0|0| ▲ player_card尚未有牌前 <br> 此時假如玩家抽到愛心6(H6),那就將玩家牌組相對應的位置改為1,並將牌組的H6改為0 | | A |2 |3 | 4 | 5 |6 | 7 | 8 |9 | 10 | J |Q | K | | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | |S|0|0|0|0|0|0|0|0|0|0|0|0|0| |H|0|0|0|0|0|1|0|0|0|0|0|0|0| |D|0|0|0|0|0|0|0|0|0|0|0|0|0| |C|0|0|0|0|0|0|0|0|0|0|0|0|0| ▲ player_card抽到愛心6 | | A |2 |3 | 4 | 5 |6 | 7 | 8 |9 | 10 | J |Q | K | | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | | S | 1 | 1 |1|1| 1 |1|1| 1 |1|1| 1 |1|1| |H| 1 | 1 |1|1| 1 |0|1| 1 |1|1| 1 |1|1| |D| 1 | 1 |1|1| 1 |1|1| 1 |1|1| 1 |1|1| |C| 1 | 1 |1|1| 1 |1|1| 1 |1|1| 1 |1|1| ▲ card愛心6被抽走 <br> ::: <br> ```cpp= #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); char COLOR[4]={'S','H','D','C'}; //花色(輸出方便) char card_number[13]={'A','2','3','4','5','6','7','8','9','10','J','Q','K'}; //A~K牌號(輸出方便) int card[4][13]; //牌組 4為花色 13為牌號 for(int i=0;i<4;i++)for(int j=0;j<13;j++)card[i][j]=1; //將牌組全設 1 代表現在牌組4種花色13張牌皆在 for(int i=0;i<4;i++){ cout << "\n" << i+1 << " 號玩家:\n" ;//輸出幾號玩家 int player_card[4][13]={0}; //玩家手上的牌 初始為零 for(int j=0;j<13;){ //j用來記玩家拿到幾張牌 int number,color; number=rand()%13; //隨機牌號 color=rand()%4; //隨機花色 if(card[color][number]==1){ //牌組是否有此花色和牌號的牌 player_card[color][number]=1; //將這張牌給玩家 card[color][number]=0; //拿走牌組的牌 j++; //拿到牌所以加一 } } int num=0; //用來計點 for(int j=0;j<4;j++){ cout << COLOR[j] << ":"; //輸出該花色 for(int k=0;k<13;k++){ if(player_card[j][k]==1){ //玩家是否有該花色第k號的牌 cout << card_number[k] << " " ;//輸出該牌號 if(k==0)num+=4;//計點 A=4 else if(k==10)num+=1; //J=1 else if(k==11)num+=2; //Q=2 else if(k==12)num+=3; //K=3 } } cout << endl; } cout << "點數為 : " << num << endl;//輸出點數 } return 0; } ``` ## 第十一CPE題 就找a,b區間是某數平方的數。 ```cpp= #include <iostream> using namespace std; int main() { int a,b; while(cin >> a >> b){ for(int i=1;i*i<=b;i++){ if(i*i>=a)cout << i*i << " "; } cout << endl; } return 0; } ``` # <font size=3 color=darkblue>10801</font> ## 第六題 輸入兩個正整數,輸出進位幾次 ```cpp= #include <iostream> using namespace std; int main() { int a,b; while(cin >> a >> b){ int c=0,ans=0; while(a!=0||b!=0){ c=(a%10+b%10+c)/10; ans=ans+c; a/=10; b/=10; } cout << ans <<endl; } return 0; } ``` ## 第十題 輸入十進位,輸出4進位 ```cpp= #include <iostream> using namespace std; int main() { int n; while (cin >> n){ int i,num[10000]={0}; for(i=0;n>0;i++){ num[i]=n%4; n/=4; i++; } for(int j=i-1;j>=0;j--){ cout << num[j]; } cout <<endl; } return 0; } ``` ## 第十三題 隨機1~100亂數,使用者輸入數字,若猜錯,提示太大OR太小,直到錯超過五次 ```cpp= #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int n; while(1){ int i,number=rand()%100+1; for(i=0;i<5;i++){ cout << "輸入1~100數字:"; cin >> n; if(number==n){ cout << "答對啦" <<endl; break; } else if(number>n)cout<< "太小\n"; else cout << "太大\n"; } if(i==5)cout << "可惜沒答對,答案是" << number <<endl; else cout << "恭喜答對啦,答案是" << number <<endl; } return 0; } ``` # <font size=3 color=darkblue>11001-02</font> ## 第十二題 設計\*A\*B 之遊戲, 規則如下: 步驟 1:由電腦隨機產生四位數供使用者猜測,其中數字不可重複,請輸出此四位數。 步驟 2:由使用者輸入所猜測的四位數。 步驟 3:若位置與數字皆正確則回應 A, 若數字正確但位置錯誤則回應 B。 步驟 4:重複以上兩個步驟,直到答對(發生 4A)或猜測超過五次為止。 ```cpp= #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main(){ srand(time(NULL)); while(1){ int ans_number[9]; for(int n=0;n<9;n++)ans_number[n]=1; int i,ans[4]={0}; cout << endl; for(int i=0;i<4;){ int a=rand()%9+1; if(ans_number[a-1]==1){ ans[i]=a; cout << ans[i]; i++; ans_number[a-1]=0; } } for(i=0;i<5;i++){ cout << "\n輸入四位數字 :"; int player_ans; cin >> player_ans; int A=0,B=0; for(int j=3;j>=0;j--){ if(ans[j]==player_ans%10)A++; else { for(int k=3;k>=0;k--){ if(j==k)continue; if(player_ans%10==ans[k]){ B++; break; } } } player_ans/=10; } if(A==4){ cout << "恭喜你答對囉,答案是 " ; for(int s=0;s<4;s++)cout << ans[s]; break; }else{ cout << "差一點, " << A << " A " << B << " B " << endl; } } if(i==5){ cout << "次數沒囉,答案是 "; for(int s=0;s<4;s++)cout << ans[s]; } } return 0; } ``` <br><Br><br> # <font size=3 color=darkblue>111年期末</font> ## 第六題 輸入長度小於100的字串,輸出出現幾次"mcu" **input(方便看清楚所以我把mcu標出來):** ``` ajhfopawio'mcu'akjflkd'mcu'afjkal ``` **output:** ``` 2次 ``` ```cpp= #include <iostream> #include <string> using namespace std; int main() { string r; while(cout << "請輸入字串:",cin >> r){ int ans=0; for(int i=0;i<100&&i<r.size();i++){ if(r[i]=='m'){ i++; if(r[i]=='c'){ i++; if(r[i]=='u')ans++; } } } cout << ans <<"次\n"; } return 0; } ``` <BR> ## 第七題(1) 輸出2022月曆 老樣子開爆(˘•ω•˘) **output:** ``` 一月 二月 三月 四月 ... 五月 六月 七月 八月 ... 九月 十月 十一月 十二月 ... ``` **code :** :::spoiler <font color=darkblue>點我</font> ```cpp= #include <iostream> using namespace std; int main() { int ans[6][28]; int n; int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int Enter[13]={0,6,13,20,27,6,13,20,27,6,13,20,27}; while(cout << "隨機輸入數:",cin >> n){ for(int i=0;i<28;i++)for(int j=0;j<6;j++)ans[j][i]=0; int enter=6,temp=0; for(int i=1;i<=4;i++){ for(int j=1;j<=mon[i];j++){ if(enter>Enter[i]){ enter=0+7*(i-1); temp++; } ans[temp][enter]=j; enter++; } enter+=7; temp=0; } enter-=28; cout << "一月 \t二月 \t三月 \t四月\n"; cout << "日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \n"; for(int i=0;i<6;i++){ for(int j=0;j<28;j++){ if(ans[i][j]==0)cout << " "; else if(ans[i][j]>=10)cout << ans[i][j] <<" "; else if(ans[i][j]<10)cout << " " << ans[i][j] <<" "; if(j==6||j==13||j==20)cout << "\t"; } cout<<endl; } cout << endl; for(int i=0;i<28;i++)for(int j=0;j<6;j++)ans[j][i]=0; for(int i=5;i<=8;i++){ for(int j=1;j<=mon[i];j++){ if(enter>Enter[i]){ enter=0+7*(i-5); temp++; } ans[temp][enter]=j; enter++; } enter+=7; temp=0; } enter-=28; cout << "五月 \t六月 \t七月 \t八月\n"; cout << "日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \n"; for(int i=0;i<6;i++){ for(int j=0;j<28;j++){ if(ans[i][j]==0)cout << " "; else if(ans[i][j]>=10)cout << ans[i][j] <<" "; else if(ans[i][j]<10)cout << " " << ans[i][j] <<" "; if(j==6||j==13||j==20)cout << "\t"; } cout<<endl; } cout << endl; for(int i=0;i<28;i++)for(int j=0;j<6;j++)ans[j][i]=0; for(int i=9;i<=12;i++){ for(int j=1;j<=mon[i];j++){ if(enter>Enter[i]){ enter=0+7*(i-9); temp++; } ans[temp][enter]=j; enter++; } enter+=7; temp=0; } cout << "九月 \t十月 \t十一月 \t十二月\n"; cout << "日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \t日 一 二 三 四 五 六 \n"; for(int i=0;i<6;i++){ for(int j=0;j<28;j++){ if(ans[i][j]==0)cout << " "; else if(ans[i][j]>=10)cout << ans[i][j] <<" "; else if(ans[i][j]<10)cout << " " << ans[i][j] <<" "; if(j==6||j==13||j==20)cout << "\t"; } cout<<endl; } } return 0; } ``` ::: <BR> ## 第七題(2) 輸入年分,輸出該年月曆 <BR> <br><br><br> # <font size=3 color=darkblue>11202-02</font> ## 第一題 ```cpp= #include <iostream> using namespace std; int main() { int n; while (cin >> n) { for (int i = 1; i <= n; i++) { for (int j = (n - i); j > 0; j--) cout << " "; for (int k = 0; k < i; k++) cout << "*"; cout << endl; } } } ``` ## 第二題 ```cpp= #include <iostream> using namespace std; int fib(int i) { if (i == 0) return 1; if (i == 1) return 1; return fib(i - 1) + fib(i - 2); } int main() { int n; while (cin >> n) { for (int i = 0; i < n; i++) { cout << fib(i) << ", "; } cout << endl; } } ``` ## 第三題 ```cpp= #include <iostream> using namespace std; bool prime(int n) { if (n == 2) return true; for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } int main() { int n; while (cin >> n) { int c = 0; for (int i = 2;; i++) { if (prime(i)) { cout << i << ", "; c++; } if (c == n) break; } } } ``` ## 第四題 ```cpp= #include <iostream> #include <string> using namespace std; int main() { int ds[] = {0, 31, 28, 31, 30, 31, 30 , 31, 31, 30, 31, 30, 31}; string s[] = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thr", "Fri"}; int m, d; // assume 2011/1/1 is saturday while (cin >> m >> d) { int days = 0; if (m > 1) for (int i = 1; i < m; i++) days += ds[i]; days += d; days -= 1; cout << s[days % 7] << endl; } } ``` ## 第五題 ```cpp= #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; int main() { string s; while (cin >> s) { string suits[] = {"S", "H", "D", "C"}; vector<int> cards(52); for (int i = 0; i < cards.size(); i++) cards[i] = i; random_shuffle(cards.begin(), cards.end()); char p = 'A'; int c = 0; for (int i = 1; i <= cards.size(); i++) { if (i % 13 == 1) cout << p++ << endl; int card_idx = i - 1; int card = cards[card_idx]; int suit = card / 13; card %= 13; cout << suits[suit]; if (card == 0) cout << "A", c += 4; else if (card == 10) cout << "J", c += 1; else if (card == 11) cout << "Q", c += 2; else if (card == 12) cout << "K", c += 3; else cout << card; cout << " "; if (i % 13 == 0) cout << endl << "points: " << c << endl, c = 0; } } } ``` ## 第六題 輸入兩個字串,先分別倒置內容,再合併一個字串輸出。 ```cpp= #include <iostream> #include <string> using namespace std; int main() { string n1,n2; while(cin >> n1 >> n2){ for(int i=0;i<n1.size();i++)cout << n1[i]; cout << endl; for(int i=0;i<n2.size();i++)cout << n2[i]; cout << endl; for(int i=0;i<n1.size();i++)cout << n1[i]; for(int i=0;i<n2.size();i++)cout << n2[i]; cout << endl; } return 0; } ``` string algorithm 解 ```cpp= #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string a, b; while (getline(cin, a) && getline(cin, b)) { reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); cout << a + b; } } ``` ## 第七題 輸入一正整數n(代表個數)及n個正整數,用動態記憶體儲存n個正整數,輸出由大到小的排序結果。 ```cpp= #include <iostream> using namespace std; int main() { int n; while(cin >> n){ int *p=new int[n]; for(int i=0;i<n;i++)cin >> p[i]; for(int i=0;i<n;i++)for(int j=i+1;j<n;j++){ if(p[i]<p[j]){ int t=p[i]; p[i]=p[j]; p[j]=t; } } for(int i=0;i<n;i++)cout << p[i]<<" "; cout <<endl; delete[] p; } return 0; } ``` algorithm 解 ```cpp= #include <iostream> #include <algorithm> using namespace std; int main() { int n; int *p; while (cin >> n) { p = new int[n]; int m; for (int i = 0; i < n; i++) cin >> m, *p = m, p++; p -= n; sort(p, p + n, greater<int>()); for (int i = 0; i < n; i++) cout << *p << ", ", p++; delete[] p; } } ``` ## 第八題 ```cpp= #include <iostream> using namespace std; class Watch { private: int hour; int minute; int second; public: Watch(int h = 0, int m = 0, int s = 0) { this -> hour = h; this -> minute = m; this -> second = s; } void print() { printf( "%02d:%02d:%02d\n", this -> hour, this -> minute, this -> second ); } void reset(int h, int m, int s) { this -> hour = h; this -> minute = m; this -> second = s; } }; int main() { Watch w1, w2(10, 25, 30); w1.print(); w2.print(); w2.reset(15, 10, 20); w2.print(); } ``` ## 第九題 ```cpp= #include <iostream> using namespace std; class Real { private: int a, b; public: Real(int n = 0, int m = 0) { this -> a = n; this -> b = m; } void input() { cin >> this -> a >> this -> b; } void add(Real R) { this -> a += R.a; this -> b += R.b; while (this -> b >= 10) this -> b %= 10, this -> a++; } void print() { cout << this -> a << "." << this -> b << endl; } }; int main() { Real r1, r2(36, 5); r1.input(); r1.add(r2); r1.print(); r2.print(); } ``` # <font size=3 color=darkblue>中山大學111程設考題(全英)</font> 有能力就試著自己寫寫看,沒能力看點英文也好 :::spoiler 題目 ![](https://i.imgur.com/S8bSbyE.jpg) ![](https://i.imgur.com/QAjYqA7.jpg) ![](https://i.imgur.com/oLqhYBJ.jpg) ![](https://i.imgur.com/Mhq87BV.jpg) ![](https://i.imgur.com/uAUFkVr.jpg) ![](https://i.imgur.com/0qxJrno.jpg) ![](https://i.imgur.com/7uLe9PI.jpg) ![](https://i.imgur.com/3lv0ncl.jpg) ![](https://i.imgur.com/6bcEbEp.jpg) ![](https://i.imgur.com/kTNNoAF.jpg) ![](https://i.imgur.com/ITwGpsq.jpg) ![](https://i.imgur.com/5LmU4Lu.jpg) ![](https://i.imgur.com/IEeYNWY.jpg) ::: <BR><BR><BR><BR><BR> # <font size=3 color=darkblue>zerojudge練習題</font> :::spoiler 難度表(點我) | 難度 | 建議花的時間 | | -------- | -------- | | ★ | 1hr ~ 2hr | | ★★ | 2hr ~ 4hr | | ★★★ | 1hr ~ 2hr | **暫時沒3星以上題目,3星建議時間比較短是因為我覺得沒人會像我一樣發瘋想好幾天題目,如果有題目比3星的難我卻打2星的,請見諒<font color= #F8F8FF>因為我覺得都很簡單(被揍) 居然看到這行你也是蠻猛</font>** ::: <br> :::spoiler 億點建議(點我) <br> **如果真的希望自己寫程式更好,建議不要直接看我的程式碼,可以先自己花時間試著寫寫看,把題目分解成好幾個小問題再解會比較容易,一題建議花個幾小時去解,至於要花多久寫可以參考難度表,嘎油~** ::: <br> ## 數字翻轉 **標籤 : 迴圈、if** **難度 : <font color=red>★☆</font>** **題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=a038) <font size=3 color=darkpink>**提示 :** * **答案開頭不能為0** * **輸入值如果為0,要輸出0** </font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> using namespace std; int main(){ int n,y; while (cin >> n){ y=0; if(n==0)cout<<0; //如果輸入值為0 輸出0 while(n){ if(n%10>0)y=1; //y用來判斷是否遇到第一個不為0的數 else if(y==0){ n/=10; continue; } cout<<n%10; n/=10; } cout << endl; } return 0; } ``` ::: <BR> ## 求數列第 n 項 **標籤 : 迴圈** **難度 : <font color=red>★</font>** **題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=b558) <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> using namespace std; int main(){ int n; while(cin >> n){ int ans=1; for(int i=0;i<n;i++){ //ans=n-1項,i=n-1 ans=ans+i; //第n項為n-1項加上n-1 } cout << ans << endl; } return 0; } ``` ::: <br> ## kevin戀愛攻略系列題-2 說好的霸王花呢?? **標籤 : 迴圈、if** **難度 : <font color=red>★☆</font>** **題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=b836) <font size=3 color=darkpink>**提示 :** * **有個測資要開long long int 才會過** * **m有可能為0,m為0時須輸出Go Kevin!!** </font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> using namespace std; int main(){ long long int n,m; //為了讓測資過所以設long long int while (cin >> n >> m){ if(m==0){ //如果m為0就輸出,m=0的意思是每次都只拔一個花瓣 cout << "Go Kevin!!" <<endl; continue; } for(long long int i=1;n>0;i+=m){ //每次都拔花瓣直到花瓣數為0或負數 n-=i; } if(n==0)cout << "Go Kevin!!" <<endl; //拔完花瓣數為0 else cout << "No Stop!!" <<endl; //拔超過了 } return 0; } ``` ::: <br> ## 忘了東西的傑克 **標籤 : 迴圈、if** **難度 : <font color=red>★</font>** **題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=b572) <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> using namespace std; int main(){ int N,H1,H2,M1,M2,M3; while(cin >> N){ //要做幾遍 for(int i=0;i<N;i++){ //N為有幾行輸入 cin >> H1 >> M1 >> H2 >> M2 >> M3; //輸入 int time=M2-M1+(H2-H1)*60; //計算前後相差時間 if(time>=M3)cout << "Yes" << endl; else cout << "No" << endl; } } return 0; } ``` ::: <br> ## 遞迴函數練習 **標籤 : 遞迴、if** **難度 : <font color=red>★★★</font>** **題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=e357) <font size=3 color=darkpink>**提示 :** * **遞迴就是函式呼叫自己** </font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> using namespace std; int f(int); int main(){ int x; while(cin >> x){ cout << f(x) << endl; } return 0; } int f(int x){ if(x==1)return 1; //x=1 f(x)=1 if(x%2==0)return f(x/2); //x=偶數 f(x)=f(x/2) else return f(x-1)+f(x+1); //x=其他 f(x)=f(x-1)+f(x+1) } ``` ::: <br> ## 免費停車 (Free Parking) **標籤 : 迴圈、if** **難度 : <font color=red>★★</font>** **題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=e621) <font size=3 color=darkpink>**提示 :** * **用迴圈去跑每一個車位是否為免費車位** </font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> using namespace std; int main (){ int n,a,b,c; while(cin >> n){ for(int i=0;i<n;i++){ cin >> a >> b >> c; int yon=0; //紀錄是否有免費車位 for(int j=a+1;j<b;j++){ if(j%c!=0){ //如果這個車位沒被c整除就輸出 cout << j << " "; yon=1; } } if(yon==0)cout << "No free parking spaces."; //沒有免費車位 cout << endl; } } } ``` ::: <br> ## 購物車(APCS題) **標籤 : 迴圈、if** **難度 : <font color=red>★★★</font>** **題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=f579) <font size=3 color=darkpink>**提示 :** * **判斷a、b商品有沒有買到** * **注意顧客可能會把商品放回去** * **不一定要用陣列** </font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> #include <cstdio> using namespace std; int main() { int a_goods,b_goods,n,take; while(cin >> a_goods >> b_goods >> n){ int ans=0; //ans=有幾個顧客買a、b商品 for(int i=0;i<n;i++){ int list[2]={0}; //陣列紀錄a與b是否有被拿取 陣列分別為a商品與b商品拿的數量 while(cin >> take && take!=0){ if(take==a_goods)list[0]++; //拿a商品 if(take==b_goods)list[1]++; //拿b商品 if(take==-a_goods)list[0]--; //a商品放回架上 if(take==-b_goods)list[1]--; //b商品放回架上 } if(list[0]>0 && list[1]>0)ans++; //a、b皆有拿 } cout << ans << endl; } return 0; } ``` ::: ## Parity(CPE一顆星題) **標籤 : 迴圈、if** **難度 : <font color=red>★★</font>** **英文題目 :** [點我](https://i.imgur.com/HnlxNkW.png) **中文題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=a132) <font size=3 color=darkpink>**提示 :** * **注意0不能輸出東西** </font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> using namespace std; int main() { int n; while(cin >> n,n!=0){ int i,ans=0,num[10000]={0}; for(i=0;n>0;i++){ num[i]=n%2; if(n%2==1)ans++; n/=2; } cout<<"The parity of "; for(i=i-1;i>=0;i--){ cout << num[i]; } cout <<" is " << ans << " (mod 2).\n"; } return 0; } ``` ::: ## Hashmat the Brave Warrior(CPE一顆星題) **標籤 : 無** **難度 : <font color=red>★</font>** **英文題目 :** [點我](https://i.imgur.com/6ozsAoe.png) **中文題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=a012) <font size=3 color=darkpink>**提示 :** * **變數最好開long long int** * **變數取絕對值用 abs(變數)** </font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> #include <cstdlib> using namespace std; int main() { long long int m,n; while(cin >> m >>n){ cout << abs(m-n) <<endl; } return 0; } ``` ::: ## Feynman **標籤 : 迴圈** **難度 : <font color=red>★</font>** **中文題目 :** [點我](https://zerojudge.tw/ShowProblem?problemid=a111) <font size=3 color=darkpink>**提示 :** * **找到公式** </font> <br> :::spoiler <font color=darkblue>**點我顯示程式碼**</font> ```cpp= #include <iostream> #include <cstdlib> using namespace std; int main() { int n; while(cin >> n,n!=0){ int ans=0; for(int i=1;i<=n;i++){ ans+=i*i; } cout << ans <<endl; } return 0; } ``` ::: ## TOI練習題 [題目](https://zerojudge.tw/ShowProblem?problemid=k466)