# c++練習 ###### tags: `computer` > 兩數交換(不使用含數) ```cpp= #include <iostream> using namespace std; int main() { int a,b ,t; cin>>a>>b; t=b; b=a; a=t; cout<<a<<b<<endl; return 0; } ``` > 兩數交換(寫出函式) ```cpp= #include <iostream> using namespace std; int main() { int x; int y; cin>>x>>y; swap(x,y); cout<<x<<" "<<y<<endl; return 0; } ``` > 判斷運勢 ```cpp= #include <iostream> using namespace std; int a,b,c; while(cin>>a>>b) { c= (a*2+b)%3; if(c==0) { cout<<"普通"<<endl; } else if(c==1) { cout<<"吉"<<endl; } else { cout<<"大吉"<<endl; } } return 0; } ``` --- > 年齡與結婚 ```cpp= #include<iostream> using namespace std; int main() { int a,b; while(cin>>a>>b) { if(a==1) { if(b>=18) { cout<<"yes"<<endl; } else { cout<<"no"<<endl; } } else if(a==2) { if(b>=16) { cout<<"yes"<<endl; } else { cout<<"no"<<endl; } } } return 0; } ``` > 質因數分解 ```cpp== #include<iostream> using namespace std; int main() { int x,i; while(cin>>x) { i=2; while(i>1) { while(x%i==0) { cout<<i<<endl; x=x/i; } i++; } } return 0; } ``` > 測試資料 ```cpp= #include<iostream> using namespace std; int main() { int n; cin>>n; while(n!=1) { cout<<n<<" "<<endl; if(n%2==0) { n=n/2; } else { n=3*n+1; } } cout<<1<<endl; return 0; } ``` --- > 列出座號 ```cpp= #include<iostream> using namespace std; int main() { int n,t; cin>>n; for( t=1;t<=n;t++) { cout<<t<<endl; } return 0; } ``` --- > 列出一堆符號 ```cpp== #include<iostream> using namespace std; int main() { int x,y,a,b; cin>>x>>y; for(a=1;a<=y;a++) { for(b=1;b<=x;b++) { cout<<"#";//不能加endl,因釀會直接換一行 } cout<<endl; } return 0; } ``` > 列出直角三角形 ```cpp= #include<iostream> using namespace std; int main() { int a,b,x,y; cin>>y; for(a=1;a<=y;a++) { for(b=1;b<=a;b++)//印出b個a。b後來不合了條件,所以跳到迴圈外面,完成第一個迴圈後又再一次從b=1開始 { cout<<"#"; } cout<<endl; } return 0; } ``` > 金字塔 高難度 ```cpp= #include<iostream> using namespace std; int main() { int a,b,x,y; cin>>y; for(a=1;a<=y;a++) { for(b=1;b<=y-a;b++)//會重複跑回圈直到數量到最大值 { cout<<" "; } for(b=1;b<=2*a-1;b++)//一樣會重複跑回圈直到數量到了 { cout<<"#"; } cout<<endl; } return 0; } ``` > 乘法表 ```cpp= #include<iostream> using namespace std; int main() { int a,b,y;//b是那一橫裡的第幾個//a是第幾行//y是要甚麼乘法表 while(cin>>y) { for(a=1;a<=y;a++) { for(b=1;b<=y;b++) { cout<<a<<"*"<<b<<"="<<a*b<<" "; } cout<<endl; } } return 0; } ``` --- ###### > ***質因數分解 有重複的質因數時無法輸出*** > #include<iostream> > using namespace std; > int main() > { > int a,x; > cout<<"請輸入一個數字"<<endl; > cin>>x; > for(a=2;a<=x;a++){ > if(x%a==0) > { > cout<<a<<" "; > x=x/a; > } > } > > return 0; > } > --- > 選擇排序法 難!!!!! ```cpp= using namespace std; int main() { int x,y,a,i; int d[1000]; cin>>a; for(i=0;i<a;i++)//i從零開始,因為陣列從[0]開始,然後i<a,不能i<=a,那樣會多一個數字要輸入 { cin>>d[i]; } for(i=0;i<a;i++)//一樣不能小於等於,如果小於等於的話會造成陣列一直多一個數字0,因為那個陣列是空的 { for(y=i+1;y<a;y++) { if(d[i]>d[y]) { swap(d[i],d[y]); } } } for(i=0;i<a;i++)//小於等於的話會多輸出一個零在最後面 { cout<<d[i]<<" "; } return 0; } ``` > 一元二次方程式 ```cpp= #include<iostream> #include <cmath> using namespace std; int main() { int x,y,a; cin>>x>>y>>a; int w=sqrt(y*y-4*x*a); if(w<0) { cout<<"No real root"; } else if(w>0) { cout<<"Two different roots x1="<<(w-y)/(2*x)<<" , "<<"x2="<<(-w-y)/(2*x); } else { cout<<"Two same roots x="<<(-y)/2*x; } return 0; } ``` > 數字顛倒 ```cpp= #include<iostream> using namespace std; int main() { int t; cin>>t; if(t==0) { cout<<0<<endl; } while(t%10==0) { t=t/10; } while(t>0) { cout<<t%10; t=t/10; } cout<<endl; } ```