# 程式設計實習C++ [題目](https://hackmd.io/@k5rBU7VZTLajqBu9N1nr5g/BJ6DznU1T) ## 20230913作業 ### 1.宣告變數a=10、b=1001.7、c='c' ``` #include<iostream> using namespace std; int main(){ int a = 10; float b = 101.7; char c = 'c'; } ``` ![](https://hackmd.io/_uploads/S1PECCCA3.png) ### 2.設計大小寫字母轉換 ``` #include<iostream> using namespace std; int main(){ char upper , lower; cout<<"請輸入大寫字母"; cin>>upper; lower = upper + 32; cout << upper << "的小寫為" << lower << endl; } ``` ![](https://hackmd.io/_uploads/rJ26k1JkT.png) ### 3.兩數互換 ``` #include<iostream> using namespace std; int main(){ int numA = 60; int numB = 80; int move; move = numB; numB = numA; numA = move; cout << numA << " " << numB << endl; return 0; } ``` ![](https://hackmd.io/_uploads/Hy-R4kJk6.png) ### 4.圓面積 ``` #include<iostream> using namespace std; int main(){ const double PI = 3.141592653589793238462643383279502884; int r; double area; cout<<"輸入半徑(公分):"; cin>>r; area = r*r*PI; cout<<"半徑"<<r<<"公分的圓面積="<<area<<"平方公分"<<endl; } ``` ![](https://hackmd.io/_uploads/SyjaFyJya.png) ### 心得 糟糕我居然跟在其他科目一樣 一行程式都沒背成功 我該不會要回去用scratch了... ## 20230920作業 ### 1.巨集常數 ``` #include<iostream> #define usa 0.0338983; #define ja 3.61011; using namespace std; int main(){ int money; cout<<"輸入金額"; cin>>money; float n1 = money*usa float n2 = money*ja cout<<"可兌換"<<n1<<"美金\n或兌換"<<n2<<"日圓"<<endl; } ``` ![](https://hackmd.io/_uploads/SkTSBOgxT.png) ### 2.換零錢程式 ``` #include<iostream> using namespace std; int main(){ int money; cout<<"輸入換幣金額"; cin>>money; int c10 = money/10 ; int c5 = money/5-c10*2; int c1 = 0-c10*10-c5*5+money; cout<<"兌換10元"<<c10<<"個 五元"<<c5 <<"個 一元"<<c1<<"個"<<endl; } ``` ![](https://hackmd.io/_uploads/ryWgSOglT.png) ### 3.溫度轉換 ``` #include<iostream> using namespace std; int main(){ float F; cout<<"輸入華氏溫度"; cin>>F; float C = (F-32)*5/9; cout<<"換算成攝氏溫度為"<<C<<"度"<<endl; } ``` ![](https://hackmd.io/_uploads/S15s4Oxgp.png) ### 4.自動販售機能接受50元、10元、5元及1元的硬幣,購買此票價之車票所需投入各種硬幣最少數量 ``` #include<iostream> using namespace std; int main(){ int money; cout<<"輸入票價"; cin>>money; int c50 = money/50; int c10 = money/10-c50*5; int c5 = money/5-c10*2-c50*10; int c1 = 0-c50*50-c10*10-c5*5+money; cout<<"票價"<<money<<"元時,需要硬幣:\n五十元 "<<c50<<" 個\n十元 "<<c10<<" 個\n五元 "<<c5<<" 個\n一元 "<<c1<<" 個"<<endl; } ``` ![](https://hackmd.io/_uploads/H1BSVuxga.png) ### 心得 跟上次的狀態比起來好一點了 不過還要多練習記憶 ``` #include<iostream> using namespace std; int main(){} ``` 這三行背不起來的基礎... 還好是電腦 複製貼上比較方便 ## 20230927作業 ### 1.心理測驗 ``` #include<iostream> using namespace std; int main(){ cout << "發生船難,你選擇留下什麼?\n選擇(1)留下老人和孕婦\n選擇(2)留下老人和嬰兒\n選擇(3)留下老人和金銀珠寶\n選擇(4)留下孕婦和嬰兒\n選擇(5)留下孕婦和金銀珠寶\n選擇(6)留下嬰兒和金銀珠寶"<<endl; int chose = 0 ; cin >> chose; cout << "輸入各選擇後回應如下:" << endl; if (chose == 1) { cout << "(1)你的情感很細膩,善於觀察細節。在感情中,相比於戀人的甜言蜜語,你更在乎對方實際的行動。" << endl; } else if(chose == 2) { cout << "(2)在感情中,你不僅洞察力超好,第六感也很強,越是親近的人這種直覺越敏銳,所以另一半對你很難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。" << endl; } else if(chose == 3) { cout << "(3)你是個典型的顏控,在擇偶時很注重另一半的外表和身高。" << endl; } else if(chose == 4) { cout << "(4)面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;另一方面你又很理性,明白現實的殘酷和金錢的重要性" << endl; } else if(chose == 5) { cout << "(5)在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。" << endl; } else if(chose == 6) { cout << "(6)在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。" << endl; } else { cout << "亂輸入,活該一輩子舔狗命!!!" << endl; } } ``` ![](https://hackmd.io/_uploads/S1ER48be6.png) ### 2.判斷座標(x,y)在第幾象限。 ``` #include<iostream> using namespace std; int main(){ int x; int y; cout<<"請輸入x值(不可輸入0):"<<endl; cin>>x; cout<<"請輸入y值(不可輸入0):"<<endl; cin>>y; if(x>0 && y>0) cout<<"("<<x<<","<<y<<")在第一象限"; else if(x<0 && y>0) cout<<"("<<x<<","<<y<<")在第二象限"; else if(x<0 && y<0) cout<<"("<<x<<","<<y<<")在第三象限"; else if(x>0 && y<0) cout<<"("<<x<<","<<y<<")在第四象限"; else cout<<"ERROR"; } ``` ![](https://hackmd.io/_uploads/B1UisLWlT.png) ### 3.用*組成三角形 ``` #include<iostream> #include <stdio.h> using namespace std; int main(){ int n; cout<<"請輸入層數:"; cin>>n; int n2; int n3; for (n2 = 1; n2 <= n; n2++) { for (n3 = n2; n3 > 0; n3--){cout<<"*"; } cout<<endl; } cout<<endl; for (n2 = n; n2 > 0; n2--) { for (n3 = n2; n3 > 0; n3--){cout<<"*"; } cout<<endl; } cout<<endl; for (n2 = 1; n2 <= n; n2++) { for (n3 = n-n2; n3 > 0; n3--){cout<<" "; } for (n3 = 1; n3 <= n2; n3++){cout<<"*"; } cout<<endl; } cout<<endl; for (n2 = n; n2 > 0; n2--) { for (n3 = n-n2; n3 > 0; n3--){cout<<" "; } for (n3 = n2; n3 > 0; n3--){cout<<"*"; } cout<<endl; } } ``` ![](https://hackmd.io/_uploads/ryY96Scxp.png) ### 4.十二生肖查詢 ``` #include<iostream> #include <stdio.h> using namespace std; int main(){ int year; cout<<"請輸入民國年:"<<endl; cin>>year; year=year%12 ; switch(year) { case 1: cout<<"鼠"; break; case 2: cout<<"牛"; break; case 3: cout<<"虎"; break; 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 12: cout<<"豬"; break; default: cout<<"ERROR"; } } ``` ![](https://hackmd.io/_uploads/rkdt-DZxp.png) ### 5.閏年 ``` #include<cmath> #include<iostream> using namespace std; int main(){ int year; cout<<"請輸入一個西元年:"; cin>>year; int y = year%4; if( y == 0 ) { cout<<year<<"年是閏年"<<endl; } else { cout<<year<<"年非閏年"<<endl; } } ``` ![](https://hackmd.io/_uploads/HJAeNX4ga.png) ### 心得 希望這次我沒又忘記什麼了... break有點麻煩,之後找找看清單和重複的程式預習好了 三角形那題好難 一直搞不清楚哪個變數是哪個 ## 20231004作業 ### 1.夏季電費計算 ``` #include<iostream> #include <stdio.h> using namespace std; int main(){ float n,prize; cout<<"夏季電費,請輸入用電度數:"; cin>>n; if(n>0){prize=1.63*n;} if(n>120) {prize=prize+0.75*(n-120);} if(n>330) {prize=prize+1.14*(n-330);} if(n>500) {prize=prize+1.28*(n-500);} if(n>700) {prize=prize+1.03*(n-700);} if(n>1000){prize=prize+1.86*(n-1000);} cout<<"本月電費為:"<<prize<<"元"; } ``` ![](https://hackmd.io/_uploads/SJzIJPqxp.png) ### 2.成績加總 ``` #include<iostream> #include <stdio.h> using namespace std; int main(){ float n,all; cout<<"輸入成績,輸入-1顯示總分"<<endl; do{ cin>>n; all = all+n; }while(n!=-1); cout<<"總分"<<all<<"分"; } ``` ![](https://hackmd.io/_uploads/r1rOZvcla.png) ### 3.判斷奇數或偶數 ``` #include<iostream> #include <stdio.h> using namespace std; int main(){ float n; cin>>n; int n2=n/2; if(n/2==n2) cout<<"偶數"; else cout<<"奇數"; } ``` ![](https://hackmd.io/_uploads/B19rQvcgT.png) ### 4.可被3整除得總和 ``` #include<iostream> #include <stdio.h> using namespace std; int main(){ cout<<"計算1到(使用者輸入)間,所有可被3整除的數值總和\n"; int n,all; cin>>n; n=n/3; for(int i = n;i>0;i--){all=all+3*n;} cout<<"總和"<<all; } ``` ![](https://hackmd.io/_uploads/HyYWDP5ep.png) ### 5.購物折扣 ``` #include<iostream> #include <bits/stdc++.h> using namespace std; int main(){ float prize,m; cout<<"商品價格"<<endl; cin>>prize; cout<<"是否為會員?(是1/否2)"; cin>>m; if(m==1){ cout<<"請問會員等級為?(鑽石會員請輸入1;白金會員請輸入2;普通會員請輸入3)"; cin>>m; if(m==1){m=0.7;} else if(m==2){m=0.8;} else{m=0.9;}} else{ cout<<"請問是否加入會員?(是1/否2)"; cin>>m; if(m==1){m=0.9;} else{m=1;}} cout<<"原價"<<prize<<"需支付"<<m*prize; } ``` ![](https://hackmd.io/_uploads/HyBdoxseT.png) ### 6.計算全班平均 ``` #include<iostream> using namespace std; int main(){ int n,score,g,all,allin; cout<<"請同學輸入全班人數:"; cin>>n; for(int i=n;i>0;i--){ cout<<"成績:"; cin>>allin; all=all+allin; if(allin > 60){g++;} } cout<<"平均成績"<<all/n<<" 及格人數"<<g<<" 不及格人數"<<n-g; } ``` ![](https://hackmd.io/_uploads/r1YEWZolp.png) ## 20231101作業 ### 1.查詢語音費率函式 ``` #include<iostream> #include<stdio.h> using namespace std; int u(int in){ switch(in){ case 196: cout<<"語音費率每秒:0.0869元"; break; case 396: cout<<"語音費率每秒:0.0869元"; break; case 796: cout<<"語音費率每秒:0.0869元"; break; default: cout<<"輸入錯誤"<<endl;} } int main(){ int input; cout<<"請輸入要查詢的月租費方案(196、396、796):"; cin>>input; u(input); } ``` ![](https://hackmd.io/_uploads/Hkh0e2fmp.png) ### 2.猜數字遊戲 ``` #include<iostream> #include <time.h> #include <stdlib.h> using namespace std; int u2(int in,int x){ while(in!=x){ cout<<"猜猜看數字是多少?(1~10):"; cin>>in; if(in > x){cout<<"猜錯了,再小一點\n";} else if(in < x){cout<<"猜錯了,再大一點\n";}}} int main(){ srand( time(NULL) ); int input,x=rand()%(10) + 1; u2(input,x); cout<<"猜對了!YA!"; } ``` ![](https://hackmd.io/_uploads/SyOXf3f76.png) ### 3.組成三角形 ``` #include<iostream> #include <time.h> #include <stdlib.h> using namespace std; int u3(int n1,int n2,int n3){ if (n1+n2>n3 && n1+n3>n2 && n2+n3>n1){ cout<<n1<<"、"<<n2<<"、"<<n3<<"可組成三角形";} else{ cout<<n1<<"、"<<n2<<"、"<<n3<<"不可組成三角形";} } int main(){ int in1,in2,in3; cout<<"請輸入三角形的三個邊長:"; cin>>in1>>in2>>in3; u3(in1,in2,in3); } ``` ![](https://hackmd.io/_uploads/Bkrzf2fmp.png) ### 4.遞迴 ``` #include<iostream> #include <time.h> #include <stdlib.h> using namespace std; int u4(int in){ if(in>0){ return in*u4(in-1);} else{return 1;} } int main(){ int n,ans; cout<<"請輸入一個整數n(n>0):"; cin>>n; ans=u4(n); cout<<"階乘"<<n<<"!:="<<ans; } ``` ![](https://hackmd.io/_uploads/Hkkwkju76.png) ### 5.將1~4題寫成一個專案型式 ``` #include <stdio.h> #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void p01(){ int in; cout<<"請輸入要查詢的月租費方案(196、396、796):"; cin>>in; switch(in){ case 196: cout<<"語音費率每秒:0.0869元"; break; case 396: cout<<"語音費率每秒:0.0869元"; break; case 796: cout<<"語音費率每秒:0.0869元"; break; default: cout<<"輸入錯誤"<<endl;}}; void p02(){ srand( time(NULL) ); int in,x=rand()%(10) + 1; while(in!=x){ cout<<"猜猜看數字是多少?(1~10):"; cin>>in; if(in > x){cout<<"猜錯了,再小一點\n";} else if(in < x){cout<<"猜錯了,再大一點\n";} } cout<<"猜對了!YA!"; }; void p03(){ int n1,n2,n3; cout<<"請輸入三角形的三個邊長:"; cin>>n1>>n2>>n3; if (n1+n2>n3 && n1+n3>n2 && n2+n3>n1){cout<<n1<<"、"<<n2<<"、"<<n3<<"可組成三角形";} else{cout<<n1<<"、"<<n2<<"、"<<n3<<"不可組成三角形";} }; int u4(int in){ if(in>0){ return in*u4(in-1);} else{return 1;}}; void p04(){ int n,ans; cout<<"請輸入一個整數n(n>0):"; cin>>n; ans=u4(n); cout<<"階乘"<<n<<"!:="<<ans; }; int main() { string menuItem[]= { "[1]查詢語音費率函式", "[2]猜數字遊戲", "[3]組成三角形", "[4]遞迴", "[0]離開" }; int i,num; int selMenu=99; while(selMenu!=0) { system("chcp 950"); //編碼 system("cls"); //清除畫面 cout<<"程式設計實習 資訊一甲 02 王言淵 \n"; cout<<"-----------------------------"<<endl; int arrLength=sizeof(menuItem)/sizeof(menuItem[0]); for(i=0; i<arrLength; i++) { cout<<menuItem[i]<<endl; } cout<<"請輸入選項:"; cin>>selMenu; switch(selMenu) { case 1: p01(); break; case 2: p02(); break; case 3: p03(); break; case 4: p04(); break; } system("PAUSE"); cout<<"\n"; } } ``` ## 1121114作業 ### 1.今彩539 ``` #include <stdio.h> #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int r(){int r=rand()%(39) + 1;return r;} void bubble(int a[],int n){ for(int i=0;i<n-1;i++) for(int j=0;j<n-1-i;j++) if(a[j]>a[j+1]) swap(a[j],a[j+1]); } int main(){ srand(time(NULL)); int g,in[5],x[5]={r(),r(),r(),r(),r()},a[5]={x[0],x[1],x[2],x[3],x[4]}; bubble(a,5); cout<<"開出順序:";for(int i=0;i<5;i++){cout<<x[i]<<" ";} cout<<"\n號碼順序:";for(int i=0;i<5;i++){cout<<a[i]<<" ";}cout<<endl; for(int i=1;i<6;i++){cout<<"請輸入號碼"<<i<<":";cin>>in[i];} for(int i=0;i<6;i++){for(int j=0;j<6;j++){ if(in[i]==a[j]){g++;a[j]=0;}}} switch(g){ case 5: cout<<"頭獎"; break; case 4: cout<<"貳獎"; break; case 3: cout<<"參獎"; break; case 2: cout<<"肆獎"; break; default: cout<<"銘謝惠顧"; break; } } ``` ![](https://hackmd.io/_uploads/BJ6IUMo46.png) ### 2.「字元陣列」依小到大(a到z)順序從螢幕輸出 ``` #include <iostream> using namespace std; int main(){ char a[10]; for(int i=0;i<10;i++) { cout<<"請輸入A["<<i<<"]的字元:"; cin>>a[i];} for(int i=0;i<10;i++){ for(int j=0;j<9-i;j++){ if(a[j]>a[j+1]){swap(a[j],a[j+1]);} } } cout<<"陣列內容由小排列至大\n"; for(int i=0;i<10;i++){cout<<a[i];} } ``` ![](https://hackmd.io/_uploads/BJ9q9Rh4p.png) ### 3.亂數函數rand()產生10整數,奇數的數加總及偶數的數加總 ``` #include <stdio.h> #include <iostream> #include <cstdlib> #include <string.h> #include <ctime> using namespace std; int main(){ srand(time(NULL)); cout<<"The numbers:"; int a[10],e,o; for(int i;i<10;i++){ a[i]=rand()%(10); cout<<a[i]<<","; } for(int i;i<10;i++){ if(a[i]%2==0){e=e+a[i];} else{o=o+a[i];} } cout<<"\nThe sum of even numbers:"<<e<<"\nThe sum of odd numbers:"<<o; } ``` ![](https://hackmd.io/_uploads/r16QvmsNa.png) ### 4.使用二維陣列存取學生的多科成績,並輸出班級成績單 ``` #include <stdio.h> #include <iostream> #include <cstdlib> #include <string.h> using namespace std; int main(){ int a[4][6],e,o; cout<<"輸入每位學生的成績(依序為國、英、數、基電、物理,以空格區隔)"<<endl; for(int i;i<4;i++){ cout<<i+1<<"號學生:"; for(int j=0;j<5;j++){ cin>>a[i][j]; a[i][5]+=a[i][j]; } } cout<<"\n班級成績單\n座號 國文 英文 數學 基電、物理 總分\n==================================\n"; for(int i;i<4;i++){ cout<<i+1<<" "; for(int j=0;j<6;j++){ cout<<a[i][j]<<" "; } cout<<endl; } } ``` ![](https://hackmd.io/_uploads/SJhXe4sNp.png) ## 1121129作業 ### 1.表格 ``` #include <stdio.h> #include <iostream> using namespace std; int main(){ int x,y; cout<<"請輸入所需列數:"; cin>>y; cout<<"請輸入所需行數:"; cin>>x; int in[x][y]; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ cout<<"請輸入表格數值iArray\["<<i<<"]["<<j<<"]:"; cin>>in[i][j]; } } for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ cout<<"iArray\["<<i<<"]["<<j<<"]:"<<in[i][j]<<" "; } cout<<endl; } } ``` ![image](https://hackmd.io/_uploads/rk66afNHp.png) ### 2.程式執行: 輸入5值存入一維陣列a,計算a中5值總和印出 ``` #include <stdio.h> #include <iostream> using namespace std; int main(){ int a[5],all; for(int i=0;i<5;i++){ cout<<"輸入值a\["<<i<<"]:"; cin>>a[i]; } for(int i=0;i<5;i++){ all+=a[i]; } cout<<"總和"<<all; } ``` ![image](https://hackmd.io/_uploads/B107QQErT.png) ### 3.程式執行: 宣告指標p,new運算子動態配置一塊記憶體輸入5值,放到指標p控制的5個連續記憶體位址印出指標p找出最大值,印出最大值及記憶體位址 ``` #include <stdio.h> #include <iostream> using namespace std; int main(){ int *p=new int[5],j; for(int i=0;i<5;i++){ cin>>*(p+i); } for(int i=0;i<5;i++){ if(*(p+i)>*(p+j)){j=i;} } cout<<"指標變數p所指向的記憶體起點:"<<p<<"\n最大值為"<<*(p+j)<<"\n最大值的記憶體位址:"<<p+j; } ``` ![image](https://hackmd.io/_uploads/BkYSbNEB6.png) ## 1121206作業 ### a004.文文的求婚 #### 題目 內容 > 文文為即將出國的珊珊送行,由於珊珊不喜歡別人給文文的那個綽號,意思就是嘲笑文文不夠聰明,但珊珊沒把握那個綽號是不是事實,所以珊珊決定考驗文文,於是告訴文文說,如果你能在我回國之前回答我生日那年是不是閏年,則等她回國後就答應他的求婚。文文抓抓腦袋想不出來,於是決定讓最擅長做運算的電腦來幫忙。 輸入說明 > 輸入有若干行直到 EOF 結束,每行包含一個整數代表年份 輸出說明 > 閏年 或 平年 #### 程式碼 ``` #include <iostream> using namespace std; int main(){ int y; while(cin>>y){ if(y%4==0&&y%100!=0||y%400==0){cout<<"閏年\n";} else{cout<<"平年\n";}}} ``` #### 項 (1)解題思路 一個if,把三個判斷式塞進去(年被4整除不被100整除,或被400整除) (2)判斷流程圖 ![](https://hackmd.io/_uploads/H1cZH9TSp.png) (3)選擇此題之原因,我學會了甚麼? 之前以為閏年只看是不是4的倍數 長知識了~ #### 參考連結📎 [原題目](https://zerojudge.tw/ShowProblem?problemid=a004) [流程圖原圖](https://zh.wikipedia.org/zh-tw/%E9%97%B0%E5%B9%B4#/media/File:%E9%96%8F%E5%B9%B4%E6%BC%94%E7%AE%97%E6%B3%95.png) ### a005.Eva的回家作業 #### 題目 內容 > Eva的家庭作業裏有很多數列填空練習。填空練習的要求是:已知數列的前四項,填出第五項。因為已經知道這些數列只可能是等差或等比數列,她決定寫一個程式來完成這些練習。 輸入說明 > 第一行是數列的數目t(0 <= t <= 20)。 以下每行均包含四個整數,表示數列的前四項。 約定數列的前五項均為不大於10⁵的自然數,等比數列的比值也是自然數。 輸出說明 > 對輸入的每個數列,輸出它的前五項。 #### 程式碼 ``` #include <iostream> using namespace std; int main(){ int n,p[5]; cin>>n; for (int i=0;i<n;i++){ cin>>p[0]>>p[1]>>p[2]>>p[3]; if (p[1]-p[0]==p[2]-p[1]){p[4]=p[3]+(p[1]-p[0]);} else{p[4]=p[3]*(p[3]/p[2]);} cout<<p[0]<<' '<<p[1]<<' '<<p[2]<<' '<<p[3]<<' '<<p[4]<< endl; }} ``` #### 項 (1)解題思路 變數n題數,佇列p數列 for(n次){ 輸入四數 確認是等差數列還是等比數列 輸出五數} (2)判斷流程圖 ![](https://hackmd.io/_uploads/SJ8r5jTSp.png) (3)選擇此題之原因,我學會了甚麼? 對於從小想用電腦小號代打四則運算作業的我,這個題目簡直是夢想實現! 學會了 電腦有點笨,寫好程式前作業都寫完了(國小也畢業了)... #### 參考連結📎 [原題目](https://zerojudge.tw/ShowProblem?problemid=a005) [流程圖](https://online.visual-paradigm.com/community/share/--1l0gsriyjv) ### a022.迴文 #### 題目 內容 > 迴文的定義為正向,反向讀到的字串均相同 > 如:abba , abcba ... 等就是迴文 > 請判斷一個字串是否是一個迴文? 輸入說明 > 輸入資料共一行包含一個字串(長度 < 1000) 輸出說明 > 針對每一行輸入字串輸出 yes or no #### 程式碼 ``` #include <iostream> #include <string> #include <algorithm> using namespace std; int main(){ string n,n2; cin>>n; n2=n; reverse(n.begin(),n.end()); if(n==n2){cout<<"yes";} else{cout<<"no";} } ``` #### 項 (1)解題思路 反轉字串 判定是否相同 (2)判斷流程圖 ![image](https://hackmd.io/_uploads/H1v45o0Ba.png) (3)選擇此題之原因,我學會了甚麼? 找了一圈怎麼把字串拆成陣列 結果居然有直接反轉字串的程式~(<algorithm>,reverse) #### 參考連結📎 [原題目](https://zerojudge.tw/ShowProblem?problemid=a022) ## 暑假作業 1. ``` #include<iostream> using namespace std; int main(){ int a,b; cin>>a>>b; cout<<a*b; } ``` 2 ``` #include<iostream> using namespace std; int main(){ int a; float b,c,d; cin>>a>>b>>c>>d; a=a*3-3+b; switch(a){ case 1:a=658;break; case 2:a=498;break; case 3:a=758;break; case 4:a=758;break; case 5:a=598;break; case 6:a=858;break; } a=a*(c+(d*0.5))*1.1; cout<<a; } ``` 3 ``` #include<iostream> using namespace std; int main(){ int in,type,out=0,done=0; cin>>in; if (in==1){//2D while(done !=2){ cin>>in; switch(in){ case 0:done++;break; case 1:type=250;break; case 2:type=280;break; case 3:type=310;break; case 4:type=240;break;} cin>>in; if (done==1&&in==0){done++;} else{done=0;out+=in*type;} } } else if(in==2){//3d while(done !=2){ cin>>in; switch(in){ case 0:done++;break; case 1:type=320;break; case 2:type=350;break; case 3:type=380;break; case 4:type=310;break;} cin>>in; if (done==1&&in==0){done++;} else{done=0;out+=in*type;} } } cout<<out; } ``` 4 ``` #include<iostream> #include<cmath> using namespace std; int main(){ int a; float b,c,d; cin>>a>>b>>c>>d; if (a==1){a=round(13.7*c+5.0*b-6.8*d+66);} else{a=round(9.6*c+1.8*b-4.7*d+655);} cout<<a; } ``` 5 ``` #include<iostream> using namespace std; int main() { int n; cin>>n; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(j==i||j== n-i-1) { cout<<"*"; }else{ cout<<" "; } } cout<<"\n"; } } ``` 6 ``` #include<iostream> using namespace std; int main(){ int a,b,c,d; cin>>a>>b>>c>>d; cout<<a*d-b*c; } ``` 7. ``` #include <iostream> #include <string> using namespace std; int main() { string id; cin >> id; if (id.length() != 10) {return false;} string a="ABCDEFGHJKLMNPQRSTUVXYWZIO"; int x,i; for(i=0;i<26;i++){ if (a[i]==id[0]){x=i+10;} } int x1=x/10,x2=x%10; int D[9]; for(i=1;i<10;i++){ D[i]=id[i]-'0'; } int c=x1+9*x2+8*D[1]+7*D[2]+6*D[3]+5*D[4]+4*D[5]+3*D[6]+2*D[7]+D[8]+D[9]; if (c%10==0) { cout<<"CORRECT"<<endl; } else { cout<<"ERROR"<<endl; } } ```