# 初入程式設計的世界
最一開始會選修程式設計,是因為從小就常聽母親說做一個程式工程師是一個很好的出路,便對程式有所嚮往,但國中因為課程問題沒能接觸到程式設計方面的活動,上了高中終於有機會步入程式設計這茫茫大海之中,探索其中奧妙。
## 第一步 認識C++
### 一.運用C++寫程式有幾需要注意
* #### 程式世界中不同形式的英文字母或符號都會有不同的意義
例如:**max**是代表取最大值的意思而**Max**便沒有特殊意義。
* #### 程式世界中的分號如果放在一行或一組程式的結尾代表著結尾,也就是句號的概念。
* #### **{ }、[ ]、( )** 使用時機不同
例如:**{ }** 在課程學習時是用來將程式碼包住,**[ ]** 在課程學習時是用來表示陣列大小,**( )** 在課程學習時是用來表示先執行。
* #### 寫程式盡量要養成縮排的習慣,要將程式整理清楚,方便以後修改。
* #### 若程式很長但編譯錯誤時可以善用**Debug** 來解決問題
## 第二步 程式碼基本架構
```c=
#include<iostream> //標頭檔-為了使用cout
using namespace std; //註明-為更方便使用cout
int main() // main()函式的開頭
{
//這一部份便是我們寫程式的區塊
return 0; //程式執行結束,回到起點,可進行下一個測資
}
```
### 寫程式最常出現的小錯誤
* 在程式的結尾沒有加上";"
* 將"="跟"=="搞混,一個為假設時所用,一個為計算時所用
* **return 0**或者**cout**擺錯位置
* a=b跟b=a是不同意義,前者會將自己變成b而b不變,後者則是相反
* for迴圈裡所int的i僅限於for迴圈,跳出for迴圈i便無意義
## 開始寫程式練習
## 一、CH1 變數與運算子
1. ### a001: 哈囉
```c=
#include <iostream>
using namespace std;
int main()
{
string s; //輸入名字
while(cin >> s)
{
cout << "hello, " << s << endl; //向S打招呼
}
return 0;
}
```
2. ### a002: 簡易加法
```c=
#include <iostream>
using namespace std;
int main()
{
int a,b; //
while(cin>>a>>b)
{
cout<<a+b<<endl;
}
return 0;
}
```
3. ### d827: 買鉛筆
```c=
#include <iostream>
using namespace std;
int main()
{
int x ;
while(cin>>x)
{
cout<<x/12*50+x%12*5<<endl;
}
return 0;
}
```
4. ### d060: 還要等多久啊?
```c=
#include <iostream>
using namespace std;
int main()
{
int m;
while(cin>>m)
{
cout<<(m<=25 ? 25-m : 85-m )<<endl;
}
return 0;
}
```
5. ### d051: 糟糕,我發燒了!
```c=
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int f;
while(cin>>f)
{
cout<<fixed<<setprecision(3)<<(f-32)/1.8<<endl;
}
return 0;
}
```
6. ### b004: 繩子上吃草的牛
```c=
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
double D,L,s,l;
double PI=2*acos(0);
while(cin>>D>>L)
{
l=L/2;
s=sqrt((L/2)*(L/2)-(D/2)*(D/2));
printf("%.3lf\n", PI*l*s);
}
}
```
7. ### d039: 11044 - Searching for Nessy
```c=
#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
while(a--)
{
int b,c;
cin >>b>>c;
cout<<(b/3)*(c/3)<<endl;
}
return 0;
}
```
8. ### d053: Big Chocolate
```c=
#include <iostream>
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{
cout<<m*n-1<<endl;
}
}
```
9. ### d277: 矩陣對角線
```c=
#include <iostream>
using namespace std;
int main()
{
int a;
while(cin>>a)
{
cout<<a-(a%2)<<endl;
}
}
```
10. ### b681: 1. 山洞探險
```c=
#include <iostream>
using namespace std;
int main()
{
int L;
while(cin>>L)
{
if(L>0)
{
cout<<L*2-1<<endl;
}
else
{
cout<<(-L)*2<<endl;
}
}
return 0;
}
```
11. ### d127: 二、牧場面積
```c=
#include <iostream>
using namespace std;
int main()
{
long long int L;
while(cin>>L)
{
if(L%4==0)
{
cout<<(L/4)*(L/4)<<endl;
}
else
{
cout<<(L/4)*(L/4+1)<<endl;
}
}
return 0;
}
```
12. ### d096: 00913 - Joana and the Odd Number
```c=
#include <iostream>
using namespace std;
int main()
{
long long int n,x;
while(cin>>n)
{
x=2*(((n+1)/2)*((n+1)/2))-1;
cout<<(x-4)+(x-2)+x<<endl;
}
return 0;
}
```
13. ### c776: 106北二1.六邊形屋瓦
```c=
#include <iostream>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
cout<<n*m*3+n*2+m*1<<endl;
}
return 0;
}
```
14. ### d549: 矩形中的几何
```c=
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double a,b,c;
while(cin>>a>>b>>c)
{
cout << fixed << setprecision(2) << sqrt(a*a + c*c - b*b) << endl;
}
return 0;
}
```
## 二、CH2 條件判斷
1. ### a012: 10055 - Hashmat the Brave Warrior
```c=
#include <iostream>
using namespace std;
int main()
{
long long int m,n,ans;
while(cin >> m >>n)
{
// 方法一
if( m>n )
{
cout << m-n << endl;
}
else
{
cout << n-m << endl;
}
// 方法二
ans=m-n;
if(ans<0)
{
ans=-ans;
}
cout << ans << endl;
}
return 0;
}
```
2. ### d065: 三人行必有我師
```c=
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
while(cin >> a >> b >> c)
{
if(a>b && a>c)
{
cout<<a<<endl;
}
else
{
if(b>c)
{
cout<<b<<endl;
}
else
{
cout<<c<<endl;
}
}
}
return 0;
}
```
3. ### a053: Sagit's 計分程式
```c=
#include <iostream>
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{
cout<<m*n-1<<endl;
}
}
```
4. ### d984: 棄保效應
```c=
#include <iostream>
using namespace std;
int main()
{
unsigned int a,b,c;
while(cin>>a>>b>>c)
{
if(a>b+c || b>a&&a>c&&a+c>b || c>a&&a>b&&a+b>c)
cout<<"A"<<endl;
else if(b>c+a || a>b&&b>c&&b+c>a || c>b&&b>a&&a+b>c)
cout<<"B"<<endl;
else
cout<<"C"<<endl;
}
return 0;
}
```
5. ### a004: 文文的求婚
```c=
#include <iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
if(n%4==0&&n%100!=0||n%400==0)
cout<<"閏年"<<endl;
else
cout<<"平年"<<endl;
}
return 0;
}
```
6. ### a273: 小朋友下樓梯
```c=
#include <iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
if(a==0&b==0)
cout<<"Ok!"<<endl;
else if(a!=0&&b==0)
cout<<"Impossib1e!"<<endl;
else if(a%b==0)
cout<<"Ok!"<<endl;
else
cout<<"Impossib1e!"<<endl;
}
}
```
7. ### a005: Eva 的回家作業
```c=
#include <iostream>
using namespace std;
int main()
{
int t,a,b,c,d;
cin>>t;
while(t--)
{
cin>>a>>b>>c>>d;
if(d-c==c-b)
{
cout << a <<" " << b<<" " << c<<" " << d <<" " << b - a + d<<endl;
}
else
{
cout << a<<" " << b<<" " << c<<" " << d<<" " << d * b<<endl;
}
}
return 0;
}
```
8. ### c461: apcs 邏輯運算子(Logic Operators)
```c=
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
while(cin>>a>>b>>c)
{
bool ans=false;
a=(a>=1?1:0);
b=(b>=1?1:0);
if((a&b)==c)
{
cout<<"AND"<<endl;
ans=true;
}
if((a|b)==c)
{
cout<<"OR"<<endl;
ans=true;
}
if((a^b)==c)
{
cout<<"XOR"<<endl;
ans=true;
}
if(ans==false)
cout<<"IMPOSSIBLE"<<endl;
}
return 0;
}
```
## 三、CH3 重複結構
1. ### d490: 我也愛偶數
```c=
#include <iostream>
using namespace std;
int main()
{
int i,n,sum;
while(cin >> n)
{
// sum=0;
// int sum=0;
for( i=1;i<=n;i++)
{
sum=sum+i;
}
cout << sum << endl;
}
return 0;
}
```
- 除錯練習2(c039)
```c=
#include <iostream>
using namespace std;
int main()
{
int n,m,maxx=0;
cin >> n >> m;
cout << n << " " << m << " ";
while(n<=m)
{
int cl=1;
while(n!=1)
{
if(n%2==1)
n=n*3+1;
else
n=n/2;
cl++;
}
maxx=max(maxx,cl);
n++;
}
cout<< maxx<<endl;
return 0;
}
```
```c=
#include <iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
int sum=0;
for(int i=a;i<=b;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
cout << sum << endl;
}
return 0;
}
```
2. d074: 電腦教室
```c=
#include <iostream>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
int maxx=-1,p;
for (int i=0;i<n;i++)
{
cin >> p;
if (p>maxx)
{
maxx=p;
}
}
cout << maxx << endl;
}
return 0;
}
```
3. ### a215: 明明愛數數
```c=
#include <iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
int sum=0,cnt=0;
do
{
sum+=a;
a++;
cnt++;
}while(sum<=b);
cout<<cnt<<endl;
}
return 0;
}
```
4. ### a024: 最大公因數(GCD)
```c=
#include <iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
int r;
do
{
r=a%b;
a=b;
b=r;
}while(r>0);
cout<<a<<endl;
}
}
```
5. ### a149: 乘乘樂
```c=
#include <iostream>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n,sum=1;
cin>>n;
do
{
sum*=n%10;
n/=10;
}while(n>0);
cout<<sum<<endl;
}
return 0;
}
```
6. ### c418: Bert的三角形 (1)
```c=
#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<<"*";
}
cout<<endl;
}
}
return 0;
}
```
7. ### c419: Bert的三角形 (2)
```c=
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
cout<<"_";
}
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
```
8. ### d498: 我不說髒話
```c=
#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
for(int i=0;i<a;i++)
{cout<<"I don't say swear words!"<<endl;}
return 0;
}
```
9. ### c022: 10783 - Odd Sum
```c=
#include <iostream>
using namespace std;
int main()
{
int n,a,b;
cin>>n;
for(int t=1;t<=n;t++)
{
cin>>a>>b;
int sum=0;
for(int i=a;i<=b;i++)
{
if(i&1)
sum+=i;
}
cout<<"Case "<<t<<": "<<sum<<endl;
}
return 0;
}
```
10. ### d010: 盈數、虧數和完全數
```c=
#include <iostream>
using namespace std;
int main()
{
int n,a,b;
while(cin>>n)
{
for(a=1;a<=(n-1);a++)
{
if(n%a==0)
b=b+a;
else
continue;
}
if(b==n)
{
cout<<"完全數"<<endl;
b=0;a=0;
}
else if(b>n)
{
cout<<"盈數"<<endl;
b=0;a=0;
}
else if(b<n)
{
cout<<"虧數"<<endl;
b=0;a=0;
}
}
return 0;
}
```
11. ### c299: 1. 連號或不連號
```c=
#include <iostream>
using namespace std;
//大到小
int main(){
int fun, r;
while( cin >> fun ){
r = 1;
int l[fun];
//輸入
for( int t = 0 ; t < fun ; t ++ ){
cin >> l[t];
}
//排序
for( int t = 0 ; t < fun - 1 ; t ++ ){
for( int t2 = 0 ; t2 < fun - 1 ; t2 ++ ){
if( l[t2] < l [t2 + 1] ){
int c = l[t2];
l[t2] = l[t2 + 1];
l[t2 + 1] = c;
}
}
}
//判斷
for( int t = 0 ; t < fun - 1 ; t ++ ){
if( l[t] - 1 != l[t + 1] ){
r = 0;
}
}
//輸出最大和最小
cout << l[fun - 1] << " " << l[0];
//對嗎?
if( r == 1 ){
cout << " yes";
} else {
cout << " no";
}
}
}
```
12. ### c420: Bert的三角形 (3)
```c=
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
cout<<"_";
}
for(int j=1;j<=i*2-1;j++)
{
cout<<"*";
}
for(int j=1;j<=n-i;j++)
{
cout<<"_";
}
cout<<endl;
}
return 0;
}
```
## 四、CH4 陣列
1. ### d212: 東東爬階梯
```c=
#include <iostream>
using namespace std;
int main()
{
int n;
unsigned long long int f[100]={1,1}; //unsigned lon long int只能到f[92]
for(int i=2;i<100;i++)
f[i]=f[i-1]+f[i-2];
while(cin >> n)
cout << f[n] << endl;
return 0;
}
```
2. ### c067: Box of Bricks
```c=
#include <iostream>
using namespace std;
int main()
{
int n,Set=1;
while(cin >> n && n>0)
{
int h[n],sum=0,ave,mov=0;
for (int i=0;i<n;i++)
{
cin >> h[i];
sum+=h[i];
}
ave=sum/n;
for (int i=0;i<n;i++)
{
if(h[i]>ave)
{
mov+=(h[i]-ave);
}
}
cout << "Set #" << Set++ << endl << "The minimum number of moves is " << mov << "." << endl << endl;
}
return 0;
}
```
3. ### a104: 排序
```c=
// insertion sort
for(int i=1;i<n;i++)
{
int key=s[i],j=i-1;
while(........) // key前面的元素(s[?])大於它,j沒有超出邊界
{
........ // key前面的元素(s[?]往右移
........ // j減1
}
s[....]=key;
}
```
4. ### d452: 二、直線最小距離和
```c=
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int s[n],sum=0;
for(int i=0;i<n;i++)
{
cin>>s[i];
}
for(int i=0;i<n-1;i++)
{
int minIdx=i;
for(int j=i+1;j<n;j++)
if(s[minIdx]>s[j])
minIdx=j;
swap(s[minIdx],s[i]);
}
for(int i=0;i<n;i++)
{
sum+=abs(s[n/2]-s[i]);
}
cout<<sum<<endl;
}
return 0;
}
```
5. ### b138: NOIP2005 1.陶陶摘苹果
```c=
#include <iostream>
using namespace std;
int main()
{
int a[10],b,sum=0;
for(int i=0;i<=9;i++)
{
cin>>a[i];
}
cin>>b;
for(int j=0;j<=9;j++)
{
if(a[j]<=(b+30))
sum+=1;
}
cout<<sum<<endl;
return 0;
}
```
6. ### b127: 會議中心(Room)
```c=
#include <iostream>
using namespace std;
int main()
{
long long int a[46],n;
a[0]=1;
a[1]=1;
for(int i=2;i<=45;i++)
{
a[i]=a[i-1]+a[i-2];
}
while(cin>>n)
{
cout<<a[n]<<endl;
}
return 0;
}
```
7. ### a034: 二進位制轉換
```c=
#include <iostream>
using namespace std;
int main()
{
int a[100];
long long int t;
while(cin>>t)
{
int i=0;
while(t)
{
a[i]=t%2;
t=t/2;
i++;
}
i-=1;
while(i>=0)
{
cout<<a[i];
i--;
}
cout<<endl;
}
return 0;
}
```
## 五、CH5 字元陣列、字串
1. ### a149: 乘乘樂
```c=
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int T;
cin >> T;
while(T--)
{
int product=1;
char n[11];
cin >> n;
for (int i=0;i<strlen(n);i++)
product*=(int)n[i]-48;
cout << product << endl;
}
return 0;
}
```
2. ### a782: 4. Redundant Acronym Syndrome Syndrome
```c=
#include <iostream>
using namespace std;
int main()
{
string line;
while(getline(cin,line) && line!="END")
{
cout<<(char)toupper(line[0]);
int last;
for(int i=1;i<=line.size();i++)
{
if(line[i]==' ')
{
cout<<(char)toupper(line[i+1]);
last=i+1;
}
}
cout<<" ";
for(int i=last;i<=line.size();i++)
{
cout<<line[i];
}
cout<<endl;
}
return 0;
}
```
3. ### a011: 00494 - Kindergarten Counting Game
```c=
#include <iostream>
using namespace std;
int main()
{
string line;
while(getline(cin,line))
{
int sum=1;
for(int i=1;i<=line.size();i++)
{
if(isalpha(line[i]) && !isalpha(line[i-1]))
sum++;
}
cout<<sum<<endl;
}
return 0;
}
```
4. ### a022: 迴文
```c=
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
string rev=s;
reverse(s.begin(),s.end());
cout<<(rev==s?"yes":"no")<<endl;
}
return 0;
}
```
5. ### a130: 12015 - Google is Feeling Lucky
```c=
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
struct rec
{
string name;
int v;
}web[10];
int t,cs=1;
cin>>t;
while(t--)
{
int maxx=0;
for(int i=0;i<10;i++)
{
cin>>web[i].name>>web[i].v;
if(web[i].v>maxx)
maxx=web[i].v;
}
printf("Case #%d:\n",cs++);
for(int i=0;i<10;i++)
if(web[i].v==maxx)
cout<<web[i].name<<endl;
}
}
```
## 六、CH6 函式
1. ### d171: 飛蛾撲火(二)
```c=
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,m;
while ( cin >> n >> m) {
cout <<floor( log10(pow(n,m)) + 1 )<< endl;
}
return 0;
}
```
2. ### c039: 00100 - The 3n + 1 problem
```c=
#include <iostream>
using namespace std;
int cl(int n)
{
int i=1;
while (n!=1)
{
if (n%2==1)
n=3*n+1;
else
n=n/2;
i++;
}
return i;
}
int main()
{
int a,b,c,d;
while(cin >> a >> b)
{
int ans=0;
c=a;
d=b;
if(a>b)
swap(a,b);
for(int i=a;i<=b;i++) // for(int i=min(a,b);i<=max(a,b);i++)
ans=max(ans,cl(i));
cout << c << " " << d << " " << ans <<endl;
}
return 0;
}
```
3. ### c294: APCS-2016-1029-1三角形辨別
```c=
#include <iostream>
#include <cstdlib>
using namespace std;
void sw(int& a,int& b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int a,b,c;
string d;
while(cin>>a>>b>>c)
{
if(a>c)
sw(a,c);
if(b>c)
sw(b,c);
if(a>b)
sw(a,b);
if(a+b<=c)
d="No";
else if((a*a+b*b)<c*c)
d="Obtuse";
else if((a*a+b*b)==c*c)
d="Right";
else if(a*a+b*b>c*c)
d="Acute";
cout<<a<<" "<<b<<" "<<c<<endl<<d<<endl;
}
return 0;
}
```
4. ### a216. 數數愛明明
```c=
#include <iostream>
#include <cmath>
using namespace std;
long long f(int n)
{
if(n==1)
return 1;
else
return n+f(n-1);
}
long long g(int n)
{
if(n==1)
return 1;
else
return f(n)+g(n-1);
}
int main()
{
int n;
while(cin>>n)
{
cout<<f(n)<<" "<<g(n)<<endl;
}
return 0;
}
```
### 學習收穫
經過這一學期的學習,我大致學會運用基本的C++語言用法,像是:while迴圈、for迴圈、do while迴圈、if條件式、陣列的用法、字元陣列的用法、函式的用法等,也解了59題的ZERO JUDG,也知道了許多程式設計的比賽及升學管道。
### 心得省思
最一開始接觸程式設計覺得滿簡單的,就是簡單的運算子跟數字,可越到後面的章節,越來越聽不太懂,因為程式越打越長,可邏輯越來越跟不上,但是我並沒有因此放棄程式設計,我開始努力解題,去了解每一道題的邏輯觀念,最後終於了解老師所教的每一段落所在執行的是甚麼,可還是會被許多題目打敗,這便是我不足的,以後要更加努力的解題目,以求進步。