# 18Dice C++解答版 :::info 嚴格抵制chatgpt複製貼上 以下程式全手寫 此為2025最新版 後面會陸續補上 by yyyy ::: ## 2.輸出函式 ### 2-1 測試系統 ```cpp=1 #include <iostream> using namespace std; int main(){ cout << "Hello, DICE!" << endl; return 0; } ``` ### 2-2 Hello, World! ```cpp=1 #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; cout << "Hello,World!"; return 0; } ``` ### 2-3 印aaa ```cpp=1 #include <iostream> using namespace std; int main() { cout << "a" << endl; cout << "aa" << endl; cout << "aaa" << endl; cout << "aaaa" << endl; cout << "aaaaa" << endl; cout << "aaaaaa" << endl; cout << "aaaaa" << endl; cout << "aaaa" << endl; cout << "aaa" << endl; cout << "aa" << endl; cout << "a" << endl; return 0; } ``` ### 2-4 不是計算 ```cpp=1 #include <iostream> using namespace std; int main() { cout << "2/3=5" << endl; cout << "2*3=5" << endl; return 0; } ``` ### 2-5 來個表格 ```cpp=1 #include <iostream> using namespace std; int main() { for (int i=1; i<=10; i++) { cout << i << "\t" << i * i << "\t" << i*i*i << endl; } return 0; } ``` ## 3.變數與指定運算子 ### 3-1 指定整數 ```cpp=1 #include<iostream> using namespace std; int main(){ int num; num=3; cout<<num<<endl; return 0; } ``` ### 3-2 加上一些形容詞 ```cpp=1 #include <iostream> using namespace std; int main() { int n = 33; cout << "There are " << n << " cats." << endl; return 0; } ``` ### 3-3 指定浮點數 ```cpp=1 #include<iostream> #include<iomanip> using namespace std; int main(){ float num1,num2; num1=10.100001; num2=5.200002; cout<<fixed<<setprecision(6)<<num2<<endl; cout<<fixed<<setprecision(6)<<num1<<endl; return 0; } ``` ### 3-4 整數與浮點數 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int h; float w; h=170; w=55.222; cout << "My height is " << h << " cm." << endl; cout << "My weight is " << fixed << setprecision(3) << w << " kg." << endl; return 0; } ``` ### 3-5 不是等號 ```cpp=1 #include <iostream> using namespace std; int main() { int a,b; a=55; b=555; a=b; b=a; cout << a << " " << b; return 0; } ``` ## 4.輸入函數 ### 4-1 1個整數 ```cpp=1 #include<iostream> using namespace std; int main(){ int num; cin >> num; cout << num; return 0; } ``` ### 4-2 3個整數 ```cpp=1 #include <iostream> using namespace std; int main() { int fi,se,th; cin >> fi >> se >> th; cout << th << " " << se << " " << fi; return 0; } ``` ### 4-3 浮點數 ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ float num1,num2; cin >> num1 >> num2; cout << fixed << setprecision(6) << num1 << endl; cout << fixed << setprecision(6) << num2; return 0; } ``` ### 4-4 3個浮點數 ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ float num1,num2,num3; cin >> num1 >> num2 >> num3; cout<<fixed<<setprecision(3)<<num3 << endl; cout<<fixed<<setprecision(2)<<num2 << endl; cout<<fixed<<setprecision(1)<<num1 << endl; return 0; } ``` ### 4-5 梯形上下底與高 ```cpp=1 #include <iostream> using namespace std; int main() { int l,w,h; cin >> l >> w >> h; cout << l << " " << w << " " << h; return 0; } ``` ## 5.算術運算子 ### 5-1 四則運算 ```cpp=1 #include<iostream> using namespace std; int main(){ int num1,num2; cin>>num1>>num2; cout<<num1<<"+"<<num2<<"="<<num1+num2<<endl; cout<<num1<<"-"<<num2<<"="<<num1-num2<<endl; cout<<num1<<"*"<<num2<<"="<<num1*num2<<endl; cout<<num1<<"/"<<num2<<"="<<num1/num2<<endl; return 0; } ``` ### 5-2 餘數 ```cpp=1 #include <iostream> using namespace std; int main() { int x,y; cin >> x >> y; cout << x << "/" << y << "=" << x/y << "餘" << x%y << endl; cout << x/y + x%y; return 0; } ``` ### 5-3 梯形面積 ```cpp=1 #include <iostream> using namespace std; int main() { int l,w,h; float a; cin >> l >> w >> h; a = (float)(l+w)*h/2; cout << "梯形面積" << a << "平方公分"; return 0; } ``` ### 5-4 溫度換算 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { float t,ans; cin >> t; ans = (float) (9.0/5.0)*t+32; cout << "華氏溫度=" << fixed << setprecision(1) << ans << endl; return 0; } ``` ### 5-5 三次溫度換算 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { float t,a,b; cin >> t >> a >> b; cout << "1.華氏溫度=" << fixed << setprecision(1) << (float) (9.0/5.0)*t+32 << endl; cout << "2.華氏溫度=" << fixed << setprecision(1) << (float) (9.0/5.0)*a+32 << endl; cout << "3.華氏溫度=" << fixed << setprecision(1) << (float) (9.0/5.0)*b+32 << endl; return 0; } ``` ### 5-6 時間計算 ```cpp=1 #include <iostream> using namespace std; int main() { int x,t; cin >> x; t = 95*x; cout << t << endl; cout << x << "個粽子共需要" << t/60 << "分" << t` << "秒" << endl; return 0; } ``` ### 5-7 圓周與面積 ```cpp=1 #include<bits/stdc++.h> using namespace std; int main(){ int r; cin >> r; cout << "圓周" << fixed << setprecision(1) << 2*r*3.14 << endl; cout << "圓的面積" << fixed << setprecision(1) << r*r*3.14; } ``` ## 6.單一選擇 ### 6-1 及格嗎? ```cpp=1 #include <iostream> using namespace std; int main() { int s; cin >> s; if(s>=60) cout << s << "分及格" << endl; cout<<"Over!" << endl; return 0; } ``` ### 6-2 正數? ```cpp=1 #include <iostream> using namespace std; int main() { int i; cin >> i; if (i>0) cout << i << "是正數" << endl; cout << "Over!"; return 0; } ``` ### 6-3 偶數? ```cpp=1 #include <iostream> using namespace std; int main() { int x; cin >> x; if (x%2==0) cout << x << "是偶數" << endl; cout << "Over!"; return 0; } ``` ### 6-4 發現不相等 ```cpp=1 #include <iostream> using namespace std; int main() { int x,y; cin >> x >> y; if (x!=y) cout << x << "!=" << y << endl; cout << "Over!"; return 0; } ``` ### 6-5 輸出正數 ```cpp=1 #include <iostream> #include <iomanip> using namespace std; int main(){ float x; cin >> x; if(x<0) cout << fixed << setprecision(2) << x*(-1) << endl; else cout << fixed << setprecision(2) << x << endl; } ``` ### 6-6 自主學習 ```cpp=1 #include <iostream> using namespace std; int main() { int i, it; int prices[5] = {0, 2300, 800, 500, 1500}; cin >> i >> it; int total = prices[i] + prices[it]; if (total > 3000) cout << "超出預算" << endl; else cout << "符合預算" << endl; return 0; } ``` ## 7.用if交換變數 ### 7-1 比大小 ```cpp=1 #include <iostream> using namespace std; int main(){ int a,b,max; cin >> a >> b; max=a; if(b>max){ max=b; } cout << "最大值" << max << endl; return 0; } ``` ### 7-2 三數比大小 ```cpp=1 #include <iostream> using namespace std; int main() { int mx,i,mn,n = 3; mx = -1; mn = 100; while(n--){ cin >> i; mx = max(i,mx); mn = min(i,mn); } cout << mx << " " << mn; return 0; } ``` ### 7-3 五數比大小 ```cpp=1 #include <iostream> using namespace std; int main() { int i,mx=-1,mn=100,n=5; while(n--){ cin >> i; mx = max(i,mx); mn = min(i,mn); } cout << mx << " " << mn; return 0; } ``` ### 7-4 自主學習 ```cpp=1 #include <iostream> using namespace std; int main() { int y; cin >> y; if (y%400==0) cout << "閏年"; else if (y%100==0 && y%400!=0) cout << "平年"; else if (y%100!=0 && y%4==0) cout << "閏年"; else cout << "平年"; return 0; } ``` ## 8.雙重選擇 ### 8-1 及格與否 ```cpp=1 #include <iostream> using namespace std; int main() { int score; cin>>score; if(score>=60) cout<<score<<"分及格\n"; else cout<<score<<"分不及格\n"; return 0; } ``` ### 8-2 基數還是偶數? ```cpp=1 #include <iostream> using namespace std; int main() { int i; cin >> i; cout << i << ": "; if (i%2==0) cout << "偶數"; else cout << "奇數"; return 0; } ``` ### 8-3 是否能夠成三角形? ```cpp=1 #include <iostream> using namespace std; int main() { int x,y,z; cin >> x >> y >> z; cout << x << " " << y << " " << z << ": "; if ( x+y>z && x+z>y && y+z>x) cout << "可以構成三角形" << endl; else cout << "不可以構成三角形" << endl; return 0; } ``` ### 8-4 是否直角三角形? ```cpp=1 #include <iostream> using namespace std; int main() { int x,y,z; cin >> x >> y >> z; if (x*x+y*y==z*z || y*y+z*z==x*x || x*x+z*z==y*y) cout << x << " " << y << " " << z << ": " << "直角三角形"; else cout << x << " " << y << " " << z << ": " << "不是直角三角形"; return 0; } ``` ### 8-5 是否與6相關? ```cpp=1 #include <iostream> using namespace std; int main() { int x; cin >> x; if (x%6==0 || x%10==6) cout << x << "符合標準"; else cout << x << "不符合標準"; return 0; } ``` ### 8-6 拆數字 ```cpp=1 #include <iostream> using namespace std; int main() { int num; cin >> num; int d1 = num % 10; int d2 = (num / 10) % 10; int d3 = num / 100; cout << d1 << d2 << d3 << endl; return 0; } ``` ### 8-7 是否為5的倍數-2? ```cpp=1 #include <iostream> using namespace std; int main() { int n; cin >> n; if((n/100)%5==0) cout << "百位數" << n/100 << "是5的倍數" << endl; else cout << "百位數" << n/100 << "不是5的倍數" << endl; if((n/10%10)%5==0) cout << "十位數" << n/10%10 << "是5的倍數" << endl; else cout << "十位數" << n/10%10 << "不是5的倍數" << endl; if((n%10)%5==0) cout << "個位數" << n%10 << "是5的倍數" << endl; else cout << "個位數" << n%10 << "不是5的倍數" << endl; return 0; } ``` ### 8-8 是否為迴文 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { string n; cin >> n; if (n[0] == n[4] && n[1] == n[3]) { cout << n << ": 是迴文" << endl; } else { cout << n << ": 不是迴文" << endl; } } ``` ### 8-9 自主學習 ```cpp=1 #include <iostream> using namespace std; int main() { int s; cin >> s; if (s==5) cout << "80分"; else if (s==7) cout << "64分"; else if (s==18) cout << "72分"; else cout << "成績不存在"; return 0; } ``` ## 9.巢狀選擇 ### 9-1 分數等第 ```cpp=1 #include <iostream> using namespace std; int main() { int s; cin >> s; if (s==5) cout << "80分"; else if (s==7) cout << "64分"; else if (s==18) cout << "72分"; else cout << "成績不存在"; return 0; } ``` ### 9-2 正三角形嗎? ```cpp=1 #include <iostream> using namespace std; int main() { int x,y,z; cin >> x >> y >> z; cout << x << " " << y << " " << z << ": "; if(x==y && y==z) cout << "正三角形"; else if (x+y>z && x+z>y && y+z>x) cout << "非正三角形"; else cout << "無法構成三角形"; return 0; } ``` ### 9-3 玩玩二分法 ```cpp=1 #include <iostream> using namespace std; int main() { long long n; cin >> n; if (n>0){ cout << n << "是正數" << endl; if(n<=10000) cout << "小於等於10000: A"; else cout << "大於10000: B"; } else{ cout << n << "是負數" << endl; if(n<=-10000) cout << "小於等於-10000: C"; else cout << "大於-10000: D"; } return 0; } ``` ### 9-4 自主學習 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { char ch; cin >> ch; if (isupper(ch)) cout << "大寫" << endl; else if (islower(ch)) cout << "小寫" << endl; else cout << "都不是" << endl; return 0; } ``` ## 10.多選一 ### 10-1 分數等第 ```cpp=1 #include <iostream> using namespace std; int main(){ int x; cin>>x; if(x>100 ||x<0) cout<<"error data"<<endl; else if(x>=90) cout<<"Your score is "<<x<<" and degree is A!\n"; else if(x>=80) cout<<"Your score is "<<x<<" and degree is B!\n"; else if(x>=70) cout<<"Your score is "<<x<<" and degree is C!\n"; else if(x>=60) cout<<"Your score is "<<x<<" and degree is D!\n"; else cout<<"Your score is "<<x<<" and degree is F!\n"; return 0; } ``` ### 10-2 點套餐 ```cpp=1 #include <iostream> using namespace std; int main() { int i; cin >> i; if (i==1) cout << "牛奶" << endl << "西瓜" << endl << "檸檬水" << endl << "吐司"; else if (i==2) cout << "西瓜" << endl << "檸檬水" << endl << "吐司"; else if (i==3) cout << "檸檬水" << endl << "吐司"; else if (i==4) cout << "吐司"; else cout << "超出範圍"; return 0; } ``` ### 10-3 象限 ```cpp=1 #include <iostream> using namespace std; int main() { float x,y; cin >> x >> y; if (x==0.0){ if (y==0.0) cout << "原點"; else cout << "y軸"; } else if(y==0.0) cout << "x軸"; else if(x>0.0){ if (y>0.0) cout << "第一象限"; else cout << "第四象限"; } else{ if (y>0.0) cout << "第二象限"; else cout << "第三象限"; } return 0; } ``` ### 10-4 年齡說 ```cpp=1 #include <iostream> using namespace std; int main() { int y; cin >> y; if (1<=y && y<=14) cout << y << "歲是小孩子"; else if (15<=y && y<=29) cout << y << "歲是志於學"; else if (30<=y && y<=39) cout << y << "歲是而立之年"; else if (40<=y && y<=49) cout << y << "歲是不惑之年"; else if (50<=y && y<=59) cout << y << "歲是知天命之年"; else if (60<=y && y<=69) cout << y << "歲是耳順之年"; else cout << y << "歲是從心所欲,不逾矩之年"; return 0; } ``` ### 10-5 三角形型別 ```cpp=1 #include <iostream> using namespace std; int main() { int x,y,z; cin >> x >> y >> z; if (x+y>z && y+z>x && x+z>y){ if (x==y && y==z) cout << x << " " << y << " " << z << ": " << "正三角形" << endl; else if (x*x+y*y==z*z || x*x+z*z==y*y || z*z+y*y==x*x) cout << x << " " << y << " " << z << ": " << "直角三角形" << endl; else if (x==y || y==z || x==z) cout << x << " " << y << " " << z << ": " << "等腰三角形" << endl; else cout << x << " " << y << " " << z << ": " << "一般三角形" << endl; } else{ cout << x << " " << y << " " << z << ": " << "無法構成三角形" << endl; } return 0; } ``` ## 11.多選一:switch ### 11-1 字元 ```cpp=1 #include <iostream> using namespace std; int main(){ char ch; cin >> ch; cout << ch; return 0; } ``` ### 11-2 點套餐 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { char s; cin >> s; switch (s) { case 'A': cout << "牛奶" << endl; cout << "西瓜" << endl; cout << "檸檬水" << endl; cout << "吐司" << endl; break; case 'B': cout << "西瓜" << endl; cout << "檸檬水" << endl; cout << "吐司" << endl; break; case 'C': cout << "檸檬水" << endl; cout << "吐司" << endl; break; default: cout << "吐司" << endl; } } ``` ### 11-3 點食物 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { char s; cin >> s; switch (s) { case 'A': case 'a': cout << "牛奶" << endl; break; case 'B': case 'b': cout << "西瓜" << endl; break; case 'C': case 'c': cout << "檸檬水" << endl; break; default: cout << "吐司" << endl; } } ``` ### 11-4 運算 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { char s; int x,y; cin >> s >> x >> y; switch (s) { case 'A': case 'a': cout << x << "+" << y << "=" << x+y << endl; break; case 'B': case 'b': cout << x << "-" << y << "=" << x-y << endl; break; case 'C': case 'c': cout << x << "*" << y << "=" << x*y << endl; break; case 'D': case 'd': cout << x << "/" << y << "=" << x/y << endl; break; default: cout << x << "%" << y << "=" << x%y << endl; } } ``` ### 11-5 運算列表 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { char s; int x,y; cin >> s >> x >> y; switch (s) { case 'A': case 'a': cout << x << "+" << y << "=" << x+y << endl; cout << x << "-" << y << "=" << x-y << endl; cout << x << "*" << y << "=" << x*y << endl; cout << x << "/" << y << "=" << x/y << endl; cout << x%y; break; case 'B': case 'b': cout << x << "-" << y << "=" << x-y << endl; cout << x << "*" << y << "=" << x*y << endl; cout << x << "/" << y << "=" << x/y << endl; cout << x%y; break; case 'C': case 'c': cout << x << "*" << y << "=" << x*y << endl; cout << x << "/" << y << "=" << x/y << endl; cout << x%y; break; case 'D': case 'd': cout << x << "/" << y << "=" << x/y << endl; cout << x%y; break; default: cout << x%y << endl; } } ``` ### 11-6 自主學習 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int a, b; char op; cin >> a >> op >> b; int result; switch (op) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; case '%': result = a % b; break; case '^': result = pow(a, b); break; case '#': result = pow(a, 1.0 / b); break; } cout << result << endl; return 0; } ``` ## 12.直覺不結構 ### 12-1 印10次 ```cpp=1 #include <iostream> using namespace std; int main(){ int i=0; repeat: cout<<"HaHaHa!\n"; i=i+1; if (i<10){ goto repeat; } return 0; } ``` ### 12-2 印N次 ```cpp=1 #include <iostream> using namespace std; int main() { int n; cin >> n; while (n--){ cout << "HaHaHa!" << endl; } return 0; } ``` ### 12-3 月份判斷 ```cpp=1 #include <iostream> using namespace std; int main() { int m; while(true){ cin >> m; if (m<=0) break; else if (m==12 || m==1 || m==2) cout << m << "月是冬天" << endl; else if (3<=m && m<=5) cout << m << "月是春天" << endl; else if (6<=m && m<=8) cout << m << "月是夏天" << endl; else if (9<=m && m<=11) cout << m << "月是秋天" << endl; else cout << "超出範圍" << endl; } return 0; } ``` ### 12-4 自主學習 ```cpp=1 #include <iostream> using namespace std; int main() { for (int i = 0; i < 5; i++) { int a1, a2, a3, a4, a5; cin >> a1 >> a2 >> a3 >> a4 >> a5; if (a1 > a2 && a2 > a3 && a3 > a4 && a4 > a5) { cout << "此數列嚴格遞減" << endl; cout << "此數列遞減" << endl; } else if (a1 >= a2 && a2 >= a3 && a3 >= a4 && a4 >= a5) { cout << "此數列遞減" << endl; } else { cout << "End" << endl; } } return 0; } ``` ## 13.重複-計數器控制 ### 13-1 基數還是偶數? ```cpp=1 #include <iostream> using namespace std; int main(){ int i,num; i=1; while(i<=10){ cin>>num; if(num%2==0){ cout<<num<<"是偶數"<<endl; } } else{ cout<<num<<"是奇數"<<endl; } i=i+1; } ``` ### 13-2 印100次 ```cpp=1 #include <iostream> using namespace std; int main() { int n=100; while(n--){ cout << "HaHaHa!印我100次" << endl; } return 0; } ``` ### 13-3 印n次 ```cpp=1 #include <iostream> using namespace std; int main() { int n; cin >> n; while(n--){ cout << "這是重複結構的固定次數範例" << endl; } return 0; } ``` ### 13-4 輸入10個浮點數 ```cpp=1 #include <iostream> #include <iomanip> using namespace std; int main() { double arr[10]; for (int i = 0; i < 10; i++) { cin >> arr[i]; } for (int i = 0; i < 10; i++) { cout << fixed << setprecision(2) << arr[i] << endl; } } ``` ### 13-5 累加 ```cpp=1 #include <iostream> using namespace std; int main() { int x,tt=0; for(int i=1; i<=10; i++){ cin >> x; tt+=x; cout << "第" << i << "次累加" << tt << endl; } cout << "累加和: " << tt; return 0; } ``` ### 13-6 輸入N個浮點數 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { double num; cin >> num; cout << fixed << setprecision(2) << num << endl; } } ``` ### 13-7 計算總合 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int n; double tt=0; cin >> n; for (int i = 0; i < n; i++) { double num; cin >> num; tt += num; } cout << fixed << setprecision(2) << tt << endl; } ``` ### 13-8 計算平均 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int n; double tt=0; cin >> n; for (int i = 0; i < n; i++) { double num; cin >> num; tt += num; } cout << fixed << setprecision(2) << tt << endl; cout << fixed << setprecision(2) << tt/n << endl; } ``` ### 13-9最高分 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int n; double mx=-1; cin >> n; for (int i = 0; i < n; i++) { double num; cin >> num; if(num>mx) mx = num; } cout << fixed << setprecision(2) << mx << endl; } ``` ### 13-10 成績計算 ```cpp=1 #include <iostream> using namespace std; int main() { int n,x,y; cin >> n; while (n--){ cin >> x >> y; cout << x+y << endl; } return 0; } ``` ### 13-11 自主學習 ```cpp=1 #include <iostream> using namespace std; int main() { int n; cin >> n; for(int i=2; i<=n; i++){ bool t=true; for(int j=2; j<i; j++){ if(i%j==0) t=false; } if(!t) cout << i << endl; } return 0; } ``` ## 14.重複-警示值控制 ### 14-1 基數還是偶數? ```cpp=1 #include <iostream> using namespace std; int main() { int num; cin >> num; while(num!=0){ if(num%2==0){ cout << num << "是偶數" <<endl; } else{ cout << num << "是奇數" << endl; } cin >> num; } return 0; } ``` ### 14-2 印到我說停 ```cpp=1 #include <iostream> using namespace std; int main() { int x; while(true){ cin >> x; if (x == 0) break; cout << "警示值範例程式!" << endl; } return 0; } ``` ### 14-3 直到我說停 ```cpp=1 #include <iostream> using namespace std; int main() { int s; while(true){ cin >> s; if(s < 0) break; cout << s << endl; } return 0; } ``` ### 14-4 重複幾次 ```cpp=1 #include <iostream> using namespace std; int main() { int s,t=0; while(true){ cin >> s; if (s < 0) break; t++; } cout << t << "筆資料"; return 0; } ``` ### 14-5 班級均分-總和 ```cpp=1 #include <iostream> using namespace std; int main() { int s,tt=0; while(true){ cin >> s; if (s<0) break; tt += s; } cout << "總和=" << tt; return 0; } ``` ### 14-6 班級均分-完成 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int s,tt=0,t=0; float ans; while(true){ cin >> s; if (s<0) break; tt += s; t += 1; } ans = (float)tt/t; cout << "總和=" << tt << endl; cout << "平均=" << fixed << setprecision(2) << ans << endl; return 0; } ``` ### 14-7 最高分 ```cpp=1 #include <iostream> using namespace std; int main() { int x,mx=-1; while(true){ cin >> x; if (x<=0) break; if (x > mx) mx = x; } cout << mx; return 0; } ``` ### 14-8 自主學習 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { while (true) { int n; cin >> n; if (n <= 1) break; bool isPrime = true; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { isPrime = false; break; } } if (isPrime) cout << "質數" << endl; else cout << "合數" << endl; } return 0; } ``` ## 15.for ### 15-1 1到100和 ```cpp=1 #include <iostream> using namespace std; int main() { int i, sum; for (i = 1, sum = 0; i <= 100; i++) { sum = sum + i; } cout << "1+2+3+...+100=" << sum << endl; return 0; } ``` ### 15-2 奇數和 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int sum = 0; for (int i = 1; i <= 99; i += 2) { sum += i; } cout << "1+3+5+...+99=" << sum << endl; return 0; } ``` ### 15-3 偶數和 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int sum = 0; for (int i = 2; i <= 100; i += 2) { sum += i; } cout << "2+4+6+...+100=" << sum << endl; return 0; } ``` ### 15-4 1到N之和 ```cpp=1 #include <iostream> using namespace std; int main() { int n,sum=0; cin >> n; for (int i=1; i<=n; i++){ sum += i; } cout << "1+2+3+...+n=" << sum; return 0; } ``` ### 15-5 X到Y之和 ```cpp=1 #include <iostream> using namespace std; int main() { int x,y,sum=0; cin >> x >> y; for (int i=x; i<=y; i++){ sum += i; } cout << "x到y的和=" << sum; return 0; } ``` ### 15-6 遞減值 ```cpp=1 #include <iostream> using namespace std; int main() { int n; cin >> n; for(int i=1000; i>0; i-=n){ cout << i << " "; } return 0; } ``` ### 15-7 公比 ```cpp=1 #include <iostream> using namespace std; int main() { int r, li; cin >> r >> li; cout << "公比數列: "; long long term = 1; // 起始值 while (term <= li) { cout << term << " "; term *= r; } return 0; } ``` ### 15-8 等比級數公式 ```cpp=1 #include <iostream> using namespace std; int main() { int a, an, r; int tt = 0; cin >> a >> an >> r; for (int i = a; i <= an; i *= r) { tt += i; } cout << "首項: " << a << endl; cout << "末項: " << an << endl; cout << "公比: " << r << endl; cout << "公比數列: " << tt << endl; return 0; } ``` ### 15-9 二的次方數 ```cpp=1 #include <iostream> using namespace std; int main() { int n; cin >> n; for (int i=1;;i*=2){ if (i>n){ cout << "第一個大於" << n << "的數是" << i; break; } } return 0; } ``` ### 15-10 自主學習 ```cpp=1 #include <iostream> using namespace std; int f(int n){ if(n==1) return 10; if(n==2) return 15; if(n==3) return 16; return f(n-2)+f(n-3); } int main() { int n; cin >> n; cout << f(n) << endl; return 0; } ``` ## 16.重複結構與判斷式 ### 16-1 及格與不及格人數 ```cpp=1 #include <iostream> using namespace std; int main() { int x,pass=0,un=0,hi=0; for(int i=1; i<=10;i++){ cin >> x; if (x>=60){ pass+=1; if (x>=80) hi+=1; } else un+=1; } cout << "及格" << pass << "人" << endl; cout << "不及格" << un << "人" << endl; cout << "超過80分" << hi << "人" << endl; return 0; } ``` ### 16-2 最大公因數 ```cpp=1 #include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int m = min(a, min(b, c)); int gc = 1; cout << a << " " << b << " " << c << "的公因數"; for (int i = 1; i <= m; i++) { if (a % i == 0 && b % i == 0 && c % i == 0) { cout << i << " "; gc = i; } } cout << endl; cout << a << " " << b << " " << c << "的最大公因數" << gc << endl; return 0; } ``` ### 16-3 if...break ```cpp=1 #include <iostream> using namespace std; int main() { int i,n,sum; cin>>n; for(i=2,sum=0;i<=n;i=i+2){ if (i % 10==0) break; cout<<i<<endl; sum=sum+i; } cout<<sum<<endl; return 0; } ``` ### 16-4 質數判斷 ```cpp=1 #include <iostream> using namespace std; int main() { int n; cin >> n; bool p = true; for (int i = 2; i < n; i++) { if (n % i == 0) { p = false; break; } } if (p) cout << n << ": 質數" << endl; else cout << n << ": 不是質數" << endl; return 0; } ``` ### 16-5 互值 ```cpp=1 #include <iostream> using namespace std; int main() { int x,y,m; bool t = true; cin >> x >> y; m = min(x,y); for (int i=2; i<=m; i++){ if (x%i==0 && y%i==0){ t=false; break; } } if (t) cout << x << " " << y << ": " << "兩數互質"; else cout << x << " " << y << ": " << "兩數不互質"; return 0; } ``` ### 16-6 if...continue ```cpp=1 #include <iostream> using namespace std; int main() { int i,n,sum; cin>>n; for(i=2,sum=0;i<=n;i=i+2){ if (i % 10==0) continue; cout<<i<<endl; sum=sum+i; } cout<<sum<<endl; return 0; } ``` ### 16-7 5的倍數不加 ```cpp=1 #include <iostream> using namespace std; int main() { int n; cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { if (i % 5 != 0) { cout << i << endl; sum += i; } } cout << sum << endl; return 0; } ``` ### 16-8 相反數絕對值-EOF ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { cout << -n << " " << abs(n) << endl; } return 0; } ``` ### 16-9 象限-EOF ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { float x,y; while (cin >> x >> y) { if (x>0 && y>0) cout << "第一象限" << endl; else if (x<0 && y>0) cout << "第二象限" << endl; else if (x<0 && y<0) cout << "第三象限" << endl; else if (x>0 && y<0) cout << "第四象限" << endl; else if (x==0 && y!=0) cout << "y軸" << endl; else if (x!=0 && y==0) cout << "x軸" << endl; else cout << "原點" << endl; } return 0; } ``` ### 16-10(C語言) ```cpp=1 #include <stdio.h> int main() { int grade,aCount=0,bCount=0,cCount=0,dCount=0,fCount=0; while((grade=getchar())!=EOF){ switch(grade){ case 'A': case 'a': ++aCount; break; case 'B': case 'b': ++bCount; break; case 'C': case 'c': ++cCount; break; case 'D': case 'd': ++dCount; break; case 'F': case 'f': ++fCount; break; default: break; } } printf("A %d人\n",aCount); printf("B %d人\n",bCount); printf("C %d人\n",cCount); printf("D %d人\n",dCount); printf("F %d人\n",fCount); return 0; } ``` ### 16-11 ```cpp=1 #include <iostream> #include <string> using namespace std; int main() { string in; cin >> in; size_t pos = in.find("GG"); if (pos != string::npos) { cout << pos + 2 << endl; } else { cout << "未找到 GG" << endl; } return 0; } ```