看板 NTUBIME100HW 關於我們 聯絡資訊
1 #include "stdafx.h" //有個值從t[0]-t[30] float t[31], sum=0.0, tmax=0.0,tmin=1000.0; #include<iostream> using namespace std; int main(){ for(int i=0;i<31;i++){ //因為陣列從開始,所以讓i從開始比較好看,但為了方便又以i為日期,日期會從-30號,為了顯示正確(不影響陣列),寫成i+1 cout<<"請輸入/"<<i+1<<"的溫度\n"; cin>>t[i]; sum=sum+t[i]; if(t[i]>tmax) {tmax=t[i];} if(t[i]<tmin) {tmin=t[i];} } cout<<"平均溫度為"<<sum/31.0<<endl; cout<<"最高溫度"<< tmax <<endl; cout<<"最低溫度"<< tmin <<endl; return 0; } 2 #include "stdafx.h" #include<iostream> using namespace std; #include<cmath> //要打#include<cmath> double y=0.0; //要用double y=0.0;而不是int int main(){ for(double x=1.0;x<=50 ;x++) { //這裡是平方,不要打成(x,x) y=y+1.0/pow(x,2); } //注意pow與sqrt不要打錯 cout<<"Pi="<<sqrt(6*y); //答案是.126,並沒有錯 return 0; } 3 #include "stdafx.h" #include<iostream> using namespace std; char input[80]; void reverse(char[]); int main() { cout<<"這個程式能顛倒字串,請輸入一段話\n"; cout<<"如果想結束程式,請按ctrl+z\n" //儲存INPUT[]的值 //注意這裡是getline,而且寫cin.getline(input,80,'\n') while(cin.getline(input,80,'\n')) //注意裡面是input reverse(input); return 0; } void reverse(char input[]) { int count=0; int i; //注意是input[i]!='\0'而不是getline for(i=0;input[i]!='\0';i++) count++; //因為i=0在跑時,count就加了一次,所以打i=count-1 for(i=count-1;i>=0;i--) cout<<input[i]; cout<<endl; //這裡打cout<<endl比較好看 } 4 #include "stdafx.h" #include<iostream> using namespace std; #include<iomanip> using std::setw; //記得using std::setw;怎麼打 //寫出前兩項 int f[20]={0,1}; int main() { //for裡面要打;而不是,請特別注意 for(int t=2;t<20;t++) { //之前要先提供個數0與,才能繼續運算出其他數 //這裡可算出個數,加上預先指定的兩個,一共是個 f[t]=f[t-1]+f[t-2]; } for(int x=1;x<20;x++) { //因為個就要換行,x從開始%5比較好計算 //因為陣列從開始,不合x從開始,所以寫成f[x-1] //要setw是因為費朋那西數列後面會很大,不預留空間數字會擠在一起 cout<<setw(10)<<f[x-1]; if(x%5==0) cout<<""<<endl; } return 0; } 5 #include "stdafx.h" #include<iostream> using namespace std; #include<cmath> int main() { int n=20; //函式原形,記得寫出所想接收函數的格式(順序會影響) double change(int,int); //for回圈是封鎖的,它能繼續提供給裡面的回圈,但不能直接給main值,定義的東西也不能給main for(int x=1;x<=10;x++) { //change立刻接x,n的值,但不能使用x,n變數 cout<<"e^"<<x<<"="<<change(x,n)<<endl; } return 0; } //因為該函式x與n沒定義所以不能在此函式亂寫 //但此時xa=x,na+n double change(int xa,int na) { //雖然xa=1,xu為double形式應為.0而不是但若不是以下狀況,不至於造成運算錯誤 double xu=xa; double e=1.0; double m=1; int yu; double t; for( yu=1;yu<na;yu++) { m=m*yu; //pow(double,int)故特地定義xu,yu的型式記得打cmath t=pow(xu,yu); //請注意相除時最容易產生錯誤,double與int的差別此時最明顯 //double x=1 double y=3 double z=x/y; 此時z=0.333 //int x=1 int y=3 double z=x/y; 此時z=0 //相除前請先將值e 分子m 分母t都化為double(只有e是不夠的) e=e+(t/m); } return e; } 6 #include "stdafx.h" #include<iostream> using namespace std; int a; int b; int r; int d=1; //為p預設一個值 int p=1; int i; void x(void); // int main(){ //為了避免重複太多次,不寫在迴圈 cout<<"這個程式能替你計算兩數間的最大公因數與最小公倍數\n"; //利用數字的簡單for迴圈 while(p!=0){ cout<<"想繼續請輸入不想繼續請輸入\n"; //以簡單的int系列,cin.get()很麻煩 cin>>i; //以i作為switch的case switch(i) { case 1: //p不變,並且計算公因數 p=1; x( ); //跳出switch,並且回到while的一開始,因為p!=0,會繼續執行 break; case 2: //跳出switch,並且回到while的一開始,因為p=0,直接結束while迴圈,到int main的return 0結束程式 p=0; break; //跳出switch,並且回到while的一開始 default: break; } } return 0; } void x(void){ cout<<"請輸入兩個數字,這個程式能替你計算兩數間的最大公因數與最小公倍數\n"; cin>>a; cin>>b; //將a與b存成另一個值來計算 int t=a; int d=b; //這裡是輾轉相除法,t%d!=0時代表找出了最大公因數 while(t%d!=0) { r=t%d ; t=d ; d=r ; } //藉由算式得r cout<<"最大公因數為"<<r; //雖然t=a,d=b不過在計算後值已經改變,所以要用a與b來算,才是以當初使用者輸入的數來計算(如果當初只用a與b算到底,會喪失ab原本的值 cout<<"最小公倍數為"<<a*b/r<<endl; } 7 #include "stdafx.h" #include<iostream> using namespace std; //使用pow() #include<cmath> int _tmain(int argc, _TCHAR* argv[]) { //注意,一定要用double,否則會除不出來 double a; double b; double c; //這裡顯示的只是字串,所以打x^2就行了 cout<<"這個程式能計算方程式ax^2+bx+c=0的解,請輸入a,b,c\n"; cout<<"a="; cin>>a; cout<<"b="; cin>>b; cout<<"c="; cin>>c; //注意預定的順序,連環的if , else if , else 易會使{}搞混 if (a==0) { if(b==0) { if (c==0) { cout<<"這個方程式無限多解\n"; } else if(c!=0) { cout<<"這個方程式無解\n"; } } else { cout<<"x="<<-(c/b)<<endl; } } else { //不能直接打b^2 double d= pow(b,2)-4*a*c; if (d>0) { //不能寫-b要寫(-1)*b //記得根號的部分要用sqrt(這裡仍然是要算數值) //+sqrt與-sqrt記得要分開寫 cout<<"x="<<(-1)*b+sqrt(d)/2*a<<endl; cout<<"x="<<(-1)*b-sqrt(d)/2*a<<endl; } else if(d==0) { cout<<"x="<<(-1)*b/2*a<<endl; } else if(d<0) { //此時有i不能運算,所以分母的+與-以及i跟/只是裝飾品 //d此時為負,記得加負號再開根號 cout<<"x="<<(-1)*b<<"+"<<sqrt(-d)<<"i"<<"/"<<2*a<<endl; cout<<"x="<<(-1)*b<<"-"<<sqrt(-d)<<"i"<<"/"<<2*a<<endl; } } return 0; } 8 #include "stdafx.h" #include<iostream> int _tmain(int argc, _TCHAR* argv[]) { //宣告變數 int N; int x; int y; int pricenumber; //使用擁有std之名者 using namespace std; //決定質數要顯示到什麼程度 cout<<"請輸入N ,這個程式能替你顯示1-N的質數\n" ; cout<<"2是一個質數\n"; cin>>N ; //x從開始,逐漸加一,一直增加到N for(x=3;x<N;x++) { //決定pricenumber的值 pricenumber=1; //y從開始,一直除x,以檢定是否為質數 for(y=2;y<x;y++) { //如果不是質數,便改變pricenumber的值 if ((x%y)==0){ pricenumber=0; } } //如果是質數,pricenumber的值則不變 if(pricenumber==1){ cout<<x<<"是一個質數\n";} } //暫停 system("pause"); return 0; } 9 #include "stdafx.h" #include<iostream> using namespace std; //寫出a[]的值個數以及值( {}內的東西) int a[15]={3,8,10,30,2,16,27,13,22,17,42,33,38,29,14}; void sin(void); //寫出函式原形 int main() { //顯示排序前的陣列 cout<<"顯示排序前的結果\n"; //a[0]~a[14] 共個值 for(int d=0; d<15;d++) { cout<<a[d]<<endl; } cout<<"顯示排序後的結果\n"; //這裡不能寫void(不必多寫一次) sin( ); return 0 ; } void sin(void){ int i,j,temp; //一共要做出次排序 for(i=0;i<14;i++) { for(j=i+1;j<15;j++) { if(a[i]>a[j]) { //替換 temp=a[i]; a[i]=a[j]; a[j]=temp; } } } //再次排出個數字 for(int d=0; d<15;d++) { cout<<a[d]<<endl; } } 10 #include "stdafx.h" #include<iostream> using namespace std; //記得rand的部分怎麼打 #include<cstdlib> using std::rand; using std::srand; //使用time寫這樣即可 #include<ctime> //將a系列所有值歸 int a[7]={0}; int _tmain(int argc, _TCHAR* argv[]) { //隨機化 srand(time(0)); for(int i=1;i<=10000;i++){ //類似以累積法改變a[]的值,由往上加 a[rand()%6+1]++; } for(int j=1;j<7;j++){ cout<<j<<"出現"<<a[j]<<"次"<<endl; } return 0; } 11 #include "stdafx.h" #include<iostream> using namespace std; int main() { class Rectangle { private: double width; double length; public: //直接回傳值當作結果 double Area(double xwidth,double xlength) { width=xwidth; length=xlength; return width*length ;} double Perimeter(double xwidth,double xlength) { //其實下面兩行可以不打,因為Area先執行,會給private的東西值,能給class中的大家用 //甚至不用參數打Perimeter(void)即可 width=xwidth; length=xlength; return 2*(width+length) ;} }; double a; double b; cout<<"請輸入長與寬\n"; cout<<"長="; cin>>a; cout<<"寬="; cin>>b; Rectangle x; //記得()內只打ab就好 cout<<"面積="<<x.Area(a,b); cout<<"周長="<<x.Perimeter(a,b); return 0; } 12 #include "stdafx.h" #include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; //預設行數列數(一定要打),以及做出歸零動作 int A[12][12]={0}; int B[12][12]={0}; int C[12][12]={0}; int M,N,L; //計算C的值 void caculate(int A[12][12],int B[12][12]) { //由於i從開始,打i<M即可 for (int i=0;i<M;i++) { for (int j=0;j<N;j++) { for (int k=0;k<L;k++) {//注意矩陣是累乘相加 C[i][j]+=A[i][k]*B[k][j]; } } } } int _tmain(int argc, _TCHAR* argv[]) { do { cout << "A[M][L] B[L][N] M,N,L<=11\n"; cout << "請輸入M= "; cin >> M; cout << "請輸入N= "; cin >> N; cout << "請輸入L= "; cin >> L; //這樣寫代表有一數大於就要求重新輸入MLN的值 do{ } while(M>11||N>11||L>11) }while(M>11||N>11||L>11); //注意do{ }while()是一組的 //計算A的值,由於預設A[12][12] A的值能少於12,但絕不能多於12 (不論是15 15 或1 都不行) for (int i=0;i<M;i++) { for (int j=0;j<L;j++) {//注意顯示i+1是因為陣列從A[0][0]開始,數學沒人這樣寫是從A[1][1]開始,不過這裡只是為了顯示方便,不影響值 cout << "A[" << i+1 << "][" << j+1 << "]= "; cin >> A[i][j]; } } //計算B的值 for (int i=0;i<L;i++) { for (int j=0;j<N;j++) { cout << "B[" << i+1 << "][" << j+1 << "]= "; cin >> B[i][j]; } } //呼叫計算C的函式 caculate(A,B); //顯示出C for (int i=0;i<M;i++) { for (int j=0;j<N;j++) { cout << setw(6) << C[i][j] << ", "; } cout << endl; } system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.194.5.42
happierway:my god!!!這我只能說大推。。。。。。 12/06 23:35
gogogo699:我PO了兩篇文才看到.....QQ 大推。。。。。。 12/06 23:54
jackieyang:感謝阿!不過第6題似乎有問題,可以幫忙看一下嗎?謝謝 12/08 02:52
jackieyang:沒事了,第六題正常,我弄錯了,抱歉 12/08 11:16
Pineapple225:驚! 原來這裡有PO了!!! XD 12/11 11:29