# 111 資訊學分搶救專區
:::info
## 前言
#### 此筆記由洪暘軒(lizer2k)編寫,為幫助111班同學順利取得資訊學科之學分而生,本筆記之內容僅供參考,***並非***標準解答,請閱覽者注意。
:::
## 第一部分:循序結構、選擇結構、重複結構
### 3.1 輸出練習
#### [程式實作] 3.1 Hello World!
```cpp=
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!"<<endl;
return 0;
}
```
#### [trytry看] 你看那有三隻綿羊呦
```cpp=
#include <iostream>
using namespace std;
int main()
{
cout<<"╭◜◝ ͡ ◜◝╮ ╭◜◝ ͡ ◜◝╮"<<endl;
cout<<"( •ω• ) ( •ω• )"<<endl;
cout<<"╰◟◞ ͜ ◟╭◜◝ ͡ ◜◝╮ ͜ ◟◞╯"<<endl;
cout<<"( •ω• )"<<endl;
cout<<"╰◟◞ ͜ ◟◞╯"<<endl;
return 0;
}
```
### 3.2 變數與運算式
#### [程式實作] 3.2.1 輸入與輸出成績
```cpp=
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"輸入資訊、數學、英文的成績";
cin>>x;
cin>>y;
cin>>z;
cout<<"資訊 "<<x<<endl;
cout<<"數學 "<<y<<endl;
cout<<"英文 "<<z<<endl;
return 0;
}
```
#### [trytry看] 西元年轉民國年
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"輸入西元年(大於1911的整數)";
cin>>n;
n=n-1911;
cout<<"民國"<<n<<"年"<<endl;
return 0;
}
```
#### [程式實作] 3.2.2 計算總分與平均
```cpp=
#include <iostream>
using namespace std;
int main()
{
float x,y,k;
cin>>x>>y;
cout<<"總分 = "<<x+y<<endl;
cout<<"平均 = "<<(x+y)/2;
return 0;
}
```
#### [程式實作] 3.2.3 計算加權總分和加權平均
```cpp=
#include <iostream>
using namespace std;
int main()
{
float x,y,z,a,b,c;
cin>>x>>y>>z;
cin>>a>>b>>c;
cout<<"總分 "<<(x*a)+(y*b)+(z*c)<<endl;
cout<<"平均 "<<((x*a)+(y*b)+(z*c))/(a+b+c);
return 0;
}
```
#### [trytry看] 輸入 5 科之測驗成績,輸出總分與平均
```cpp=
#include <iostream>
using namespace std;
int main()
{
float x,y,z,a,b;
cin>>x>>y>>z>>a>>b;
cout<<"總分 "<<x+y+z+a+b<<endl;
cout<<"平均 "<<(x+y+z+a+b)/5;
return 0;
}
```
#### [程式實作] 3.2.4 圓面積與周長
```cpp=
#include <iostream>
using namespace std;
int main()
{
float x;
cin>>x;
cout<<"面積 "<<x*x*3.14159;
cout<<" 周長 "<<x*2*3.14159;
return 0;
}
```
#### [trytry看] 梯形的面積
```cpp=
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
float ar;
cin>>a>>b>>c;
ar=(a+b)*c;
ar=ar/2;
cout<<"面積 "<<ar;
return 0;
}
```
#### [程式實作] 3.2.5 購物折扣
```cpp=
#include <iostream>
using namespace std;
int main()
{
int money;
cin>>money;
if (money>=1000)
money=money-(100*(money/1000));
cout<<"應付金額 "<<money;
return 0;
}
```
#### [程式實作] 3.2.6 分組問題
```cpp=
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"輸入座號";
cin>>x;
x=(x-1)/3+1;
cout<<"第"<<x<<"組";
return 0;
}
```
### 3.3 循序結構與選擇結構
#### [程式實作] 3.3.1 兩數交換
```cpp=
#include <iostream>
using namespace std;
int main()
{
int x,y,t;
cin>>x>>y;
t=x;
x=y;
y=t;
cout<<"x="<<x<<" "<<"y="<<y;
return 0;
}
```
#### [程式實作] 3.3.2 59 分自動加 1 分
```cpp=
#include <iostream>
using namespace std;
int main()
{
int x;
cin>>x;
if (x==59)
x++;
cout<<"成績"<<" "<<x;
return 0;
}
```
#### [程式實作] 3.3.3 三數之最大數
##### \*這題我用的做法不同,請各位不要當作考試參考
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[3]={0},i;
for (i=0;i<3;i++)
cin>>a[i];
sort(a,a+3);
cout<<"最大數為 "<<a[2];
return 0;
}
```
#### [trytry看] 計算購物折扣
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x;
cin>>x;
if (x>=4000)
x=x*0.8;
else if (x>=3000)
x=x*0.85;
else if (x>=2000)
x=x*0.9;
else if (x>=1000)
x=x*0.95;
cout<<"實付金額 "<<x;
return 0;
}
```
#### [trytry看] 五整數之最小者
##### \*這題我用的做法不同,請各位不要當作考試參考
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[5]={0},i;
for (i=0;i<5;i++)
cin>>a[i];
sort(a,a+5);
cout<<"最小數 "<<a[0];
return 0;
}
```
#### [trytry看] 三角形三邊長求面積
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main()
{
float a,b,c,area,s;
cin>>a>>b>>c;
if ((a+b>c) && (b+c>a) && (a+c>b))
{
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"土地總價為 "<<area;
}
else
cout<<"無法構成三角形三邊長";
}
```
### 3.4 重複結構
#### [程式實作] 3.4.1 好多個hello c++
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
for (i=1;i<=n;i++)
cout<<"hello c++";
return 0;
}
```
#### [程式實作] 3.4.2計算 1 加到 n 的值
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,k=0;
cin>>n;
for (i=1;i<=n;i++)
k=k+i;
cout<<k;
return 0;
}
```
#### [trytry看] 3.4.3 +1+3+5+7...+n
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,k=0;
cin>>n;
for (i=1;i<=n;i=i+2)
k=k+i;
cout<<k;
return 0;
}
```
#### [trytry看] 3.4.4 +2+4+6+8...+n
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,k=0;
cin>>n;
for (i=2;i<=n;i=i+2)
k=k+i;
cout<<k;
return 0;
}
```
#### [程式實作] 3.4.5 計算總分、平均與不及格科數
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,k=0,a;
float avg,s;
cin>>n;
for (i=1;i<=n;i++)
{
cin>>a;
s=s+a;
if (a<60)
k++;
}
avg=s/n;
cout<<"總分 "<<s<<" 平均 "<<avg<<" 不及格科數 "<<k;
return 0;
}
```
#### [trytry看] 印出星號三角形
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,j;
cin>>n;
for (i=5;i>=1;i--)
{
for (j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
return 0;
}
```
#### [程式實作] 3.4.6 印出星號三角形、正三角形
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,j;
cin>>n;
for (i=1;i<=5;i++)
{
for (j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
return 0;
}
```
#### [程式實作] 3.4.4while 迴圈的應用
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,s;
cin>>n>>s;
i=1;
while (n<s)
{
n=n*1.05;
cout<<"完成第 "<<i<<" 個作業的成績 "<<n<<endl;
i++;
}
cout<<"要多完成的作業數 "<<i-1;
return 0;
}
```
### 加分題
#### [加分題] 生成2-100的質數表
##### \*這題老師給的測資有問題,答案是對的
```cpp=
#include <iostream>
using namespace std;
int main() {
int start = 2, end = 100, n;
bool ifPrime = true;
while (start < end) {
ifPrime = true;
if (start == 0 || start == 1) {
ifPrime = false;
}
for (n = 2; n <= start/2; ++n) {
if (start % n == 0) {
ifPrime = false;
break;
}
}
if (ifPrime)
cout << start <<"\t";
++start;
}
return 0;
}
```
#### [加分題] 九九乘法表
```cpp=
#include <iostream>
using namespace std;
int main()
{
int x,y,z,i,j;
for (i=2;i<=9;i++)
{
for (j=1;j<=9;j++)
cout<<i<<"*"<<j<<"="<<i*j<<"\t";
cout<<endl;
}
return 0;
}
```
#### [加分題] 王老師的占卜
```cpp=
#include <iostream>
using namespace std;
int main()
{
int m,d,s;
cin>>m>>d;
s=(m*2+d)%5;
if(s==0)
{
cout<<"大凶";
}
else if(s==1)
{
cout<<"凶";
}
else if(s==2)
{
cout<<"普通";
}
else if(s==3)
{
cout<<"吉";
}
else if(s==4)
{
cout<<"大吉";
}
return 0;
}
```
#### [加分題] 絕對值
```cpp=
#include <iostream>
using namespace std;
int main()
{
int m,d,s;
cin>>m;
if (m<0)
m=m*(-1);
cout<<m;
return 0;
}
```
#### [加分題] 身體質量指數
```cpp=
#include <iostream>
using namespace std;
int main()
{
float bmi,w,h;
cin>>h;
cin>>w;
h=h/100;
bmi=w/(h*h);
if (bmi<18.5)
cout<<"BMI "<<bmi<<endl<<"過輕";
else if (bmi>=18.5 && bmi<24)
cout<<"BMI "<<bmi<<endl<<"正常範圍";
else if (bmi>=24 && bmi<27)
cout<<"BMI "<<bmi<<endl<<"過重";
else if (bmi>=27 && bmi<30)
cout<<"BMI "<<bmi<<endl<<"輕度肥胖";
else if (bmi>=30 && bmi<35)
cout<<"BMI "<<bmi<<endl<<"中度肥胖";
else if (bmi>=35)
cout<<"BMI "<<bmi<<endl<<"重度肥胖";
return 0;
}
```
#### [加分題] 糖果分裝
```cpp=
#include <iostream>
using namespace std;
int main()
{
for (int i=1;i<=3000;i++)
if (i%7==5 && i%11==5 && i%17==5)
cout<<i<<endl;
return 0;
}
```
#### [加分題] 儲蓄計畫
```cpp=
#include <iostream>
using namespace std;
int main()
{
float n,i,k=1,s;
cin>>n>>s;
i=0;
while (i<s)
{
cout<<"第"<<k<<"個月存 "<<n<<" 元"<<endl;
i=i+n;
n=n*1.1;
k++;
}
cout<<"總額 "<<i<<" 元";
return 0;
}
```
#### [加分題] 計算總分、平均、不及格科數、不及格成績加
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,k=0,a;
float avg,s;
cin>>n;
cout<<n<<" ";
for (i=1;i<=n;i++)
{
cin>>a;
s=s+a;
if (a<60)
{
k++;
cout<<"*"<<a<<" ";
}
else if (a>=60)
cout<<a<<" ";
}
avg=s/n;
cout<<endl<<"總分 "<<s<<" 平均 "<<avg<<" 不及格科數 "<<k;
return 0;
}
```
## 【回家作業】
#### 第一題:王大同與Hello C++的愛情故事
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
int k;
cin>>k;
if (k!=0){
for (int i;i<=k;i++)
cout<<"Hello C++"<<endl;
cout<<"別擔心王大同,你的 C++ 程式碼比你想像中還要好,就像我們的關係一樣,一點一點進步!";
}
else
cout<<"王大同今日不練習程式,但你知道嗎?你已經是我心中的 C++ 大師了!";
return 0;
}
```
#### 第二題:交換巫師玩交換
```cpp=
#include <iostream>
using namespace std;
int main()
{
int x,y,t;
cin>>x>>y;
if (x==y)
cout<<"你耍我嗎!?";
else
cout<<y<<" "<<x;
return 0;
}
```
#### 第三題:閏年
```cpp=
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
cin >> y ;
if(y % 400 == 0){
cout << "閏年" << endl;
}
else if(y % 100 == 0)
{
cout << "平年" << endl;
}
else if(y % 4 == 0)
{
cout << "閏年" << endl;
}
else
{
cout << "平年" << endl;
}
}
```
#### 第四題:王大同的hp codeWars
```cpp=
#include <iostream>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
int q = x / 3;
int o = x % 3;
int p = (y - 1) / 3 + 1;
cout << q << " " << o << " " << p << endl;
return 0;
}
```
#### 第五題:大笨鳥的數學挑戰
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,i,k=0;
cin>>n;
for (i=2;i<=n;i=i+2)
k=k+i;
cout<<k;
return 0;
}
```
#### 第六題:葬送的芙莉蓮
```cpp=
#include <iostream>
using namespace std;
int main() {
int finalX, finalY;
int soulX, soulY;
cin >> finalX >> finalY;
cin >> soulX >> soulY;
if (finalX == soulX && finalY == soulY) {
cout << "找到欣梅爾的魂魄,芙莉蓮表達了她未能在生前說完的話語。" << endl;
} else if (finalX == soulX && finalY != soulY) {
cout << "沒有找到魂魄,但在這次冒險中芙莉蓮更了解了人類情感。" << endl;
} else if (finalX != soulX && finalY == soulY) {
cout << "沒有找到魂魄,但這次冒險讓芙莉蓮對人類產生了更多好奇。" << endl;
} else {
cout << "芙莉蓮未能在這次冒險中找到欣梅爾的魂魄,她仍然感到遺憾。" << endl;
}
return 0;
}
```
#### 第七題:凱文的神秘之旅
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main()
{
float a,b,c,s,n;
int area;
cin>>n;
for (int i=1;i<=n;i++){
cin>>a>>b>>c;
if ((a+b>c) && (b+c>a) && (a+c>b))
{
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<area<<endl;
}
else
cout<<"0"<<endl;
}
}
```
#### 第八題:偶數和
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if (a > b) {
int temp = a;
a = b;
b = temp;
}
int sum = 0;
for (int i = a; i <= b; ++i) {
if (i % 2 == 0) {
sum += i;
}
}
cout << sum << endl;
return 0;
}
```
#### 第九題:找因數
```cpp=
#include <iostream>
using namespace std;
int main()
{
int n,m,i,k;
cin>>n;
k=0;
m=1;
for (int i=1;i<=n;i=i+1)
{
if (n%i == 0)
cout<<i<<" ";
}
return 0;
}
```
#### 第十題: BMI 值計算
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
float n=0;
float h,w,bmi,r[5][2] = {
{166,66},
{175,68},
{172,64},
{169,58},
{180,55},
};
cout<<"編號\t身高\t體重\t狀態"<<endl;
for (int i=0;i<5;i++)
{
cout<<i+1<<"\t";
for (int j=0;j<=1;j++)
{
cout<<r[i][j]<<"\t";
}
h=r[i][0];
w=r[i][1];
h=h/100;
bmi=w/(h*h);
if (bmi<18.5)
cout<<"過輕";
else if (bmi>=18.5 && bmi<24)
cout<<"正常範圍";
else if (bmi>=24 && bmi<27)
cout<<"過重";
else if (bmi>=27 && bmi<30)
cout<<"輕度肥胖";
else if (bmi>=30 && bmi<35)
cout<<"中度肥胖";
else if (bmi>=35)
cout<<"重度肥胖";
n=0;
cout<<endl;
}
return 0;
}
```