# ITSA E-tutor 程式碼參考(數學1) {%hackmd BJrTq20hE %} ## [數學 I(Mathematics)](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/index.php?id=284) > [name=風思] 新增 [Python版本](https://hackmd.io/GsvoEIfMQAy6qtg-4gCDSA) <- 比較漂亮 > ~~看到以前寫的,感覺自己好菜喔,連i都因為學C的緣故沒直接在for迴圈裡宣告。~~ > [time=Wed, Jun 30, 2021 10:20 PM] --- > [name=風思]有幾題難題以井字及紅字標誌,可先跳過,其中涉及較複雜的演算法。 > [time=Fri, Aug 21, 2020 9:32 PM] --- > [name=風思] 就當參考吧~反正我C++也剛學1個月(x > 程式碼力求簡潔,讓別人看懂也是不容易的。 > 結束43題~ > [time=Sat, Dec 14, 2019 10:47 PM] --- > [TOC] ### [C_MM01-易] 計算梯型面積 #### C++: ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { //宣告三數分別代表上底、下底、高 int a,b,c; //輸入 cin >> a >> b >> c; //計算梯形面積 double area = ((a+b)*c)/2.0; //四捨五入取到小數點第一位 cout << fixed << setprecision(1) << "Trapezoid area:" << area << endl; return 0; } ``` ### [C_MM02-易] 計算三角形面積 #### C++: ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { //宣告兩數分別代表底、高 int a,b; //輸入 cin >> a >> b; //計算面積 double area = (a*b)/2.0; //四捨五入取到小數點第一位 cout << fixed << setprecision(1) << area << endl; return 0; } ``` ### [C_MM03-易] 兩數總和 #### C++: ```cpp= #include <iostream> using namespace std; int main() { //宣告兩數 int a,b; //重複輸入 while(cin >> a >> b){ cout << a+b << endl; } return 0; } ``` ### [C_MM04-易] 計算總和、乘積、差、商和餘數 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int a,b; cin >> a >> b; cout << a << "+" << b << "=" << a+b << endl; cout << a << "*" << b << "=" << a*b << endl; cout << a << "-" << b << "=" << a-b << endl; //餘數>=0 int c=a%b; if(c<0) cout << a << "/" << b << "=" << (a/b)-1 << "..." << c+b <<endl; else cout << a << "/" << b << "=" << (a/b) << "..." << c <<endl; return 0; } ``` ### [C_MM05-易] 計算正方形面積 #### C++: ```cpp= #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { double a; cin >> a ; //round函式四捨五入到個數位,故乘10後round,再除10.0回來 cout << fixed << setprecision(1) << round(a*a*10)/10.0 << endl; return 0; } ``` ### [C_MM06-易] 英哩轉公里 #### C++: ```cpp= #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int mile; cin >> mile; double km = mile * 1.6; cout << fixed << setprecision(1) << km << endl; return 0; } ``` ### [C_MM07-易] 計算平方值與立方值 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x; cin >> x; cout << x << " " << x*x << " " << x*x*x << endl; return 0; } ``` ### [C_MM08-易] 計算兩數和的平方值 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x,y,z; cin >> x >> y; z=x+y; cout << z*z << endl; return 0; } ``` ### [C_MM09-易] 計算 i 次方的值 #### C++: ##### <特解>解法一: ```cpp= #include <iostream> using namespace std; int main() { int i,result=1; cin >> i; if(i>31) cout << "Value of more than 31" << endl; else { //位移運算元,每往左即乘2 result = result << i; cout << result << endl; } return 0; } ``` ##### <直觀>解法二: ```cpp= #include <iostream> using namespace std; int main() { int i,j,result=1; cin >> i; if(i>31) cout << "Value of more than 31" << endl; else { //使用迴圈 for(j=1; j<=i; j++) result *=2; cout << result << endl; } return 0; } ``` #### <直觀>解法三: ```cpp= #include <iostream> #include <cmath> using namespace std; int main() { int i,result; cin >> i; if(i>31) cout << "Value of more than 31" << endl; else { //使用cmath中的pow函數 result=pow(2,i); cout << result << endl; } return 0; } ``` ### [C_MM10-易] 攝氏溫度轉華式溫度 #### C++: ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { double c,f; cin >> c; f=c*(9.0/5.0)+32; cout << fixed << setprecision(1) << f << endl; return 0; } ``` ### [C_MM11-易] 購票計算 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int n,n10,n5,n1; cin >> n; //%表取餘數,而int間相除仍為整數 n10=n/10; n5=(n%10)/5; n1=n%5; cout << "NT10=" << n10 << endl; cout << "NT5=" << n5 << endl; cout << "NT1=" << n1 << endl; return 0; } ``` ### [C_MM12-易] 相遇時間計算 #### C++: ```cpp= #include <iostream> #include <cmath> using namespace std; int main() { //dist表distance,t表秒數 double dist,t; cin >> dist; //換成公分 dist *=100; t=dist/(100-30*2.54); //無條件進位 cout << ceil(t) << endl; return 0; } ``` ### [C_MM13-易] 停車費計算 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int h1,h2,m1,m2,t,sum=0;; cin >> h1 >> m1 >> h2 >> m2; t = 60*(h2-h1)+(m2-m1); if(t<=120 and t>=30) sum=(t/30)*30; else if(t>120 and t<=240) sum=((t-120)/30)*40+120; else if(t>240) sum=((t-240)/30)*60+280; cout << sum << endl; return 0; } ``` ### [C_MM14-易] 計算時間的組合 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int s,days,hours,minutes,seconds; cin >> s; days = s/86400; hours = (s%86400)/3600; minutes = (s%3600)/60; seconds = s%60; cout << days << " days" << endl; cout << hours << " hours" << endl; cout << minutes << " minutes" << endl; cout << seconds << " seconds" << endl; return 0; } ``` ### [C_MM15-易] 判斷座標是否在正方形的範圍內 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x,y; cin >> x >> y; if(x>=0 and x<=100 and y>=0 and y<=100) cout << "inside" << endl; else cout << "outside" << endl; return 0; } ``` ### [C_MM16-易] 判斷座標是否在圓形的範圍內 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x,y; cin >> x >> y; if(x*x<=10000 and y*y<=10000) cout << "inside" << endl; else cout << "outside" << endl; return 0; } ``` ### [C_MM17-易] 求最大公因數 #### C++: #### <直觀>解法一(迴圈): 取小數開始,較有效率。 ```cpp= #include <iostream> using namespace std; int main() { int x,y,temp,i; cin >> x >> y; //設定x為較大的數 if(y>x) { temp=x; x=y; y=temp; } for(i=y; i>=1; i--) { if(x%i==0 and y%i==0) { cout << i << endl; break; } } return 0; } ``` #### <推薦>解法二(遞迴): 輾轉相除法(gcd)滿足交換律,gcd(x,y)=gcd(y,x),不必管x與y的大小關係。 ```cpp= #include <iostream> using namespace std; int gcd(int x,int y) { if(x%y==0) return y; else return gcd(y,x%y); } int main() { int x,y,result; cin >> x >> y; result=gcd(x,y); cout << result << endl; return 0; } ``` ### [C_MM18-易] 十進制轉二進制 #### C++: "~"表[取一補數(ones' complement)](https://zh.wikipedia.org/wiki/%E4%B8%80%E8%A3%9C%E6%95%B8),人腦處理負數轉[二進位](https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%BF%9B%E5%88%B6)時用[二補數(2's complement)](https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%A3%9C%E6%95%B8),但我們用陣列操作起來不方便,所以我便有了以下想法: > [name=風思] ~~以下開放膜拜:~~ > [color=#5165c6] ```cpp= #include <iostream> using namespace std; int main() { int x,i; int result[8]; cin >> x; bool isneg = (x<0); //負數的話則取一的補數,轉成正數處理 if(isneg) x=~x; //實踐二進位,倒置放入陣列 for(i=0; i<8; i++) { result[7-i]=x%2; x/=2; } //再轉回來 if(isneg) { for(i=0; i<8; i++) { if(result[i]==0) result[i]=1; else result[i]=0; } } //輸出,最後換行 for(i=0; i<8; i++) cout << result[i]; cout << "\n"; return 0; } ``` ### [C_MM19-易] 電話費計算 #### C++: ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { int t; double sum=0; cin >> t; if(t<=800) sum=t*0.9; else if(t>800 and t<1500) sum=(t*0.9)*0.9; else if(t>=1500) sum=(t*0.9)*0.79; cout << fixed << setprecision(1) << sum << endl; return 0; } ``` ### [C_MM20-易] 十進位轉十六進位 #### C++: hex表16進位輸出,uppercase表大寫輸出。 ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { int x; cin >> x; cout << hex << uppercase << x << endl; return 0; } ``` ### [C_MM21-易] 算階乘 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int n,i; //int型態會溢位 long sum=1; cin >> n; for(i=1; i<=n; i++) sum*=i; cout << sum << endl; return 0; } ``` ### [C_MM24-易] 計算薪水 #### C++: ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { int hour,pay; double salary=0; cin >> hour >> pay; if(hour<=60) salary=hour*pay; else if(hour>60 and hour<121) salary=(60*pay)+(hour-60)*pay*1.33; else if(hour>=121) salary=(60*pay*2.33)+(hour-120)*pay*1.66; cout << fixed << setprecision(1) << salary << endl; return 0; } ``` ### [C_MM25-易] 計算正整數被3整除之數值之總和 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int N,i,sum=0; cin >> N; for(i=1; i<=N; i++) { if(i%3==0) sum+=i; } cout << sum << endl; return 0; } ``` ### [C_MM26-易] 輸出 1*1、2*2、...、N*N之結果 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int N,i; cin >> N; for(i=1; i<=N; i++) cout << i << "*" << i << "=" << i*i << endl; return 0; } ``` ### [C_MM27-易] 計算兩整數間所有整數的總和 #### C++: #### <特解>解法一(純數): abs取絕對值 ```cpp= #include <iostream> #include <cmath> using namespace std; int main() { int a,b; cin >> a >> b; int sum= ((a+b)*(abs(a-b)+1))/2; cout << sum << endl; return 0; } ``` #### <直觀>解法二(迴圈): ```cpp= #include <iostream> using namespace std; int main() { int a,b,i,temp,sum=0; cin >> a >> b; //設定b為大數 if(a>b) { temp=a; a=b; b=temp; } for(i=a; i<=b; i++) sum+=i; cout << sum << endl; return 0; } ``` ### [C_MM28-易] 計算1到N之間屬於5和7的倍數 #### C++: 最後一個不能有空格,且記得換行。 ```cpp= #include <iostream> using namespace std; int main() { int N,i; cin >> N; for(i=1; i<=N; i++) { if(i%35==0) { if(i==35) cout << i; else cout << " " << i; } } cout << "\n"; return 0; } ``` ### [C_MM29-易] 最大質數問題 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x,i,j; bool isprime; cin >> x; for(i=x-1; i>=2; i--) { isprime=true; for(j=2; j<i; j++) { if(i%j==0) { isprime=false; break; } } if(isprime) { cout << i << endl; break; } } return 0; } ``` ### [C_MM30-易] 質數判別 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x,i; bool isprime=true; cin >> x; for(i=2; i<x; i++) { if(x%i==0) { isprime=false; break; } } if(isprime) cout << "YES" << endl; else cout << "NO" << endl; return 0; } ``` ### [C_MM31-易] 計算1~N內能被2跟3整除,但不能被12整除的整數總和 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int N,i,sum=0; cin >> N; for(i=1; i<=N; i++) { if(i%6==0 and i%12!=0) sum+=i; } cout << sum << endl; return 0; } ``` ### [C_MM32-易] Armstrong數 #### C++: ```cpp= #include <iostream> #include <cmath> using namespace std; int main() { int x,a,b,c; cin >> x; a=x/100; b=(x%100)/10; c=x%10; bool isArmstrong=(pow(a,3)+pow(b,3)+pow(c,3))==x; if(isArmstrong) cout << "Yes" << endl; else cout << "No" << endl; return 0; } ``` ### [C_MM33-易] 找1~N的完美數 #### C++: 第一個完美數為6,且完美數必為偶數。 ```cpp= #include <iostream> using namespace std; int main() { int N,i,j,sum; cin >> N; for(i=6; i<=N; i+=2) { sum=0; for(j=1; j<i; j++) { if(i%j==0) sum+=j; } if(sum==i) { if(i==6) cout << i; else cout << " " << i; } } cout << "\n"; return 0; } ``` ### [C_MM34-易] 因數問題 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x,i; cin >>x; for(i=1; i<=x; i++) { if(x%i==0) { if(i==1) cout << i; else cout << " " << i; } } cout << "\n"; return 0; } ``` ### [C_MM35-易] 平、閏年判定 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int year; cin >> year; if(year%4==0) { if(year%400==0) cout << "Bissextile Year" << endl; else cout << "Common Year" << endl; } else cout << "Common Year" << endl; return 0; } ``` ### [C_MM36-易] 季節判定 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int month; cin >> month; switch (month) { case 3: case 4: case 5: cout << "Spring" << endl; break; case 6: case 7: case 8: cout << "Summer" << endl; break; case 9: case 10: case 11: cout << "Autumn" << endl; break; case 12: case 1: case 2: cout << "Winter" << endl; break; } return 0; } ``` ### [C_MM37-易] 判斷座標位於何處 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x,y; cin >> x >> y; if(x==0 and y==0) cout << "Origin" << endl; else if(x!=0 and y==0) cout << "x-axis" << endl; else if(x==0 and y!=0) cout << "y-axis" << endl; else if(x>0 and y>0) cout << "1st Quadrant" << endl; else if(x<0 and y>0) cout << "2nd Quadrant" << endl; else if(x<0 and y<0) cout << "3rd Quadrant" << endl; else if(x>0 and y<0) cout << "4th Quadrant" << endl; return 0; } ``` ### [C_MM38-易] 判斷3整數是否能構成三角形之三邊長 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int x,y,z; cin >> x >> y >> z; if(x+y>z and x+z>y and y+z>x) cout << "fit" << endl; else cout << "unfit" << endl; return 0; } ``` ### [C_MM39-易] 判斷是何種三角形 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int a,b,c,a2,b2,c2; cin >> a >> b >> c; a2=a*a; b2=b*b; c2=c*c; if(a+b>c and a+c>b and b+c>a) { if(a2+b2==c2 or a2+c2==b2 or b2+c2==a2) cout << "Right Triangle" << endl; else if(a2+b2<c2 or a2+c2<b2 or b2+c2<a2) cout << "Obtuse Triangle" << endl; else if(a2+b2>c2 and a2+c2>b2 and b2+c2>a2) cout << "Acute Triangle" << endl; } else cout << "Not Triangle" << endl; return 0; } ``` ### [C_MM40-易] 1~N之間的總和 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int N,i,sum=0; cin >> N; for(i=1; i<=N; i++) { if(i==1) cout << i << " "; else cout << "+ " << i << " "; sum+=i; } cout << "= " << sum << endl; return 0; } ``` ### [C_MM42-中] 求(-1)^(n+1)*[1/(2n-1)]的和 #### C++: ```cpp= #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int n,i; double S=0; cin >> n; for(i=1; i<=n; i++) S+=pow(-1.0,(i+1)*1.0)*1/(2*i-1); cout << fixed << setprecision(3) << S << endl; return 0; } ``` ### [C_MM44-易] The Numbers #### C++: ```cpp= #include <iostream> using namespace std; int main() { char N[2]; char M[8]; int count=0; cin >> N; cin >> M; for(int i=0; i<=6; i++) { if(M[i]==N[0] and M[i+1]==N[1]) count++; } cout << count << endl; return 0; } ``` ### <font color="#f00">#[C_MM45-中] 分禮物</font> #### Java: ```java= import java.util.*; class P { int n; int[] a; int[] b; int k; Human[] arr; StringBuffer strBuf; P(int n) { this.n = n; this.k = 0; strBuf = new StringBuffer(); a = new int[n + 1]; b = new int[n + 1]; arr = new Human[n + 1]; } void dfs(int step) { // 完成一組 if (step == n + 1) { // 判斷有沒有選到自己重複的 for (int i = 1; i <= n; i++) { if (arr[i].gift.equals(arr[a[i]].gift)) return; } for (int i = 1; i <= n; i++) { if (i != 1) this.strBuf.append(","); this.strBuf.append(arr[i].name + " " + arr[a[i]].gift); } strBuf.append("\n"); k++; return; } for (int i = 1; i <= n; i++) { if (b[i] == 0) { a[step] = i; b[i] = 1; dfs(step + 1); b[i] = 0; } } return; } void ans() { System.out.println(this.k); System.out.print(this.strBuf); } } class Human { String name; String gift; } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); P p = new P(n); for (int i = 1; i <= n; i++) { p.arr[i] = new Human(); p.arr[i].name = sc.next(); p.arr[i].gift = sc.next(); } p.dfs(1); p.ans(); } } ``` ### [C_MM46-易] 複數運算 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int n,a,b,c,d; char optor; cin >> n; while(n-->0) { cin >> optor >> a >> b >> c >> d; if (optor == '+') cout << (a + c) << " " << (b + d) << endl; else if (optor == '-') cout << (a - c) << " " << (b - d) << endl; else if (optor == '*') cout << (a * c) - (b * d) << " " << (b * c) + (a * d) << endl; else if (optor == '/') cout << ((a * c) + (b * d)) / (c * c + d * d) << " " << ((b * c) - (a * d)) / (c * c + d * d) << endl; } return 0; } ``` ### [C_MM48-易] F91 #### C++: ```cpp= #include <iostream> using namespace std; int f91(int n) { if(n<=100) return f91(f91(n+11)); else if(n>=101) return n-10; } int main() { int k,n; cin >> k; while(k-->0) { cin >>n; cout << f91(n) << endl; } return 0; } ``` ### [C_MM49-易] 連續1的倍數 #### C++: ```cpp= #include <iostream> using namespace std; int main() { int k, n, num=1, count = 1; cin >> k; while(k-->0) { cin >> n; for(; (num %= n)!=0; count++) { num = 10*num+1; } cout << count << endl; num = 1; count = 1; } return 0; } ``` ### <font color="#f00">#[C_MM50-易] 分贓 </font> #### C++: ```cpp= #include <bits/stdc++.h> #define _for(i,a,b) for(int i=a;i<b;i++) using namespace std; int readInt(){ int x; cin >> x; return x; } int main(){ int k=readInt(); int total=0,half; int w[k]; _for(i,0,k) total+=w[i]=readInt(); half=total/2; int bag[half+1]; memset(bag,0,sizeof(bag)); _for(i,0,k) for(int j=half;j>=w[i];j--) bag[j]=max(bag[j-w[i]]+w[i],bag[j]); cout << (int)abs(total-2*bag[half]) << endl; return 0; } ``` #### Java: ```java= import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); int[] w = new int[k]; int total = 0; for (int i = 0; i < k; i++) total += w[i] = sc.nextInt(); int half = total / 2; int[] bag = new int[half + 1]; for (int i = 0; i < k; i++) for (int j = half; j >= w[i]; j--) bag[j] = Math.max(bag[j - w[i]] + w[i], bag[j]); System.out.println(Math.abs(total - 2 * bag[half])); } } ``` ###### tags: `e tutor` `數學 I(Mathematics)` > [name=風思] 當作練習C++~ > [color=#770aaa]