引用 | 編輯
唐老鴨
2005-05-29 17:48 |
3樓
▲ ▼ |
下面是引用allenmail於2005-05-29 15:07發表的 : 因為你的題目還算有趣.... 所以幫你寫了部分的程式.... 主要的消去法已經幫你寫好了... 剩下就是判斷妳題目的解.... 我是依照妳的題目來寫的.... 所以這題的答案是無解.... 後面的判斷寫法很簡單... 就留給你自己寫囉.... PS.//測試宣告和//進度測試的迴圈你可以殺掉...我只是測試用的...記的自己把< i >改成[ i ].... 複製程式 #include <iostream.h> #include <iomanip.h> void main() { int i,j,k; float temp; float arg[3][3] = {{1,2,1},{2,1,-1},{-1,1,2}}; float sol[3] = {4,-1,3}; //測試宣告 for (i=0;i<3;i++) { for (j=0;j<3;j++) { cout<<setw(4)<<arg[i][j]; } cout<<setw(4)<<sol[i]<<endl; } //主要演算法 for (i=0;i<3;i++) { for (j=0;j<3;j++) { if (i!=j) { temp = arg[i][i] / arg[j][i]; for (k=0;k<3;k++) { arg[j][k] *= temp; arg[j][k] = arg[i][k] -arg[j][k]; } sol[j] *= temp; sol[j] = sol[i] - sol[j]; } } //進度測試 cout<<endl<<endl; for (int x=0;x<3;x++) { for(int y=0;y<3;y++) { cout<<setw(6)<<arg[x][y]; } cout<<setw(6)<<sol[x]<<endl; } } //這邊加入妳判斷的部分 } x2 |
引用 | 編輯
allenmail
2005-06-22 15:14 |
6樓
▲ ▼ |
#include <stdio.h>
main(void) { int t1=0,t2=0,t3=0,j; double arg[3][4] = {{1,2,1,4},{2,1,-1,-1},{-1,1,2,3}}; double temp[3],temp2[3]; if(arg[0][0]==0){ if(arg[1][0]!=0){ for (j=0;j<4;j++) { temp2[j]=arg[1][j]; arg[1][j]=arg[0][j]; arg[0][j]=temp2[j]; } } if(arg[2][0]!=0){ for (j=0;j<4;j++) { temp2[j]=arg[2][j]; arg[2][j]=arg[0][j]; arg[0][j]=temp2[j]; } } else{ t1=1; } } if(t1==0){ temp[0]=arg[0][0]; for (j=0;j<4;j++) { arg[0][j]=arg[0][j]/temp[0]; } for (j=3;j>=0;j--) { arg[1][j]=arg[1][j]-arg[1][0]*arg[0][j]; arg[2][j]=arg[2][j]-arg[2][0]*arg[0][j]; } } if(arg[1][1]==0){ if(arg[2][1]!=0){ for (j=0;j<4;j++) { temp2[j]=arg[2][j]; arg[2][j]=arg[1][j]; arg[1][j]=temp2[j]; } } else{ t2=1; } } if(t2==0){ temp[1]=arg[1][1]; for (j=0;j<4;j++) { arg[1][j]=arg[1][j]/temp[1]; } for (j=3;j>=0;j--) { arg[0][j]=arg[0][j]-arg[0][1]*arg[1][j]; arg[2][j]=arg[2][j]-arg[2][1]*arg[1][j]; } } if(arg[2][2]==0){ t3=1; } if(t3==0){ temp[2]=arg[2][2]; for (j=0;j<4;j++) { arg[2][j]=arg[2][j]/temp[2]; } for (j=3;j>=0;j--) { arg[0][j]=arg[0][j]-arg[0][2]*arg[2][j]; arg[1][j]=arg[1][j]-arg[1][2]*arg[2][j]; } } printf("the solution is x,y,z = "); if(t1==1 || t2==1 || t3==1){ printf("not single solution"); } else{ for (j=0;j<3;j++) { printf("%.3lf ,",arg[j][3]); } } printf("\n"); return(0); } x0 |