Gaussian demeta Template

Source: Internet
Author: User

According to the czyuan template, I tried it and saved it.

It seems that today, I have read about Gaussian metadata Elimination all day, read the template, and I have already typed it again.

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <algorithm> 7 # define LL _ int64 8 const int maxn = 100 + 10; 9 Using namespace STD; 10 int equ, VAR, FN; 11 int A [maxn] [maxn], X [maxn]; 12 bool free_x [maxn]; 13 14 int gcd (int A, int B) 15 {16 return B = 0? A: gcd (B, A % B); 17} 18 int lcm (int A, int B) 19 {20 return a * B/gcd (A, B ); 21} 22 int Gauss () 23 {24 int I, J, K, max_r, Col; 25 int Ta, TB, LCM, TMP, fx_num; 26 int free_index; 27 Col = 0; 28 29 for (k = 0; k <equ & Col <var; k ++, Col ++) 30 {31 max_r = K; 32 For (I = k + 1; I <equ; I ++) 33 If (ABS (A [I] [col])> ABS (A [max_r] [col]) 34 max_r = I; 35 36 IF (max_r! = K) 37 for (j = K; j <var + 1; j ++) 38 swap (A [k] [J], a [max_r] [J]); 39 40 if (a [k] [col] = 0) 41 {42 k --; 43 continue; 44} 45 for (I = k + 1; I <equ; I ++) 46 {47 if (a [I] [col]! = 0) 48 {49 LCM = lcm (ABS (A [I] [col]), ABS (A [k] [col]); 50 TA = LCM/ABS (A [I] [col]); 51 TB = LCM/ABS (A [k] [col]); 52 if (a [I] [col] * A [k] [col] <0) TB =-TB; 53 54 for (j = Col; j <var + 1; j ++) 55 A [I] [J] = A [I] [J] * Ta-A [k] [J] * TB; 56} 57} 58} 59 for (I = K; I <equ; I ++) 60 if (a [I] [col]! = 0) 61 Return-1; 62 63 If (k <var) 64 {65 for (I = K-1; I> = 0; I --) 66 {67 fx_num = 0; 68 for (j = 0; j <var; j ++) 69 if (a [I] [J]! = 0 & free_x [J]) 70 {71 fx_num ++; 72 free_index = J; 73} 74 if (fx_num> 1) continue; 75 76 TMP = A [I] [Var]; 77 for (j = 0; j <var; j ++) 78 if (a [I] [J]! = 0 & J! = Free_index) 79 TMP-= A [I] [J] * X [J]; 80 81 X [free_index] = tmp/A [I] [free_index]; 82 free_x [free_index] = 0; 83} 84 return var-K; 85} 86 for (I = var-1; I> = 0; I --) 87 {88 TMP = A [I] [Var]; 89 for (j = I + 1; j <var; j ++) 90 if (a [I] [J]! = 0) 91 TMP-= A [I] [J] * X [J]; 92 93 If (TMP % A [I] [I]! = 0) Return-2; 94 X [I] = tmp/A [I] [I]; 95} 96 return 0; 97} 98 int main () 99 {100 int I, j; 101 while (CIN> equ> var) 102 {103 memset (A, 0, sizeof (a); 104 memset (X, 0, sizeof (x); 105 memset (free_x, 1, sizeof (free_x); 106 for (I = 0; I <equ; I ++) 107 {108 For (j = 0; j <var + 1; j ++) 109 CIN> A [I] [J]; 110} 111 112 fn = Gauss (); 113 If (fn =-1) cout <"unsolvable" <Endl; 114 else if (fn =-2) 115 cout <"floating point solution and integer solution" <<Endl; 116 else if (FN> 0) 117 {118 printf ("infinite solution! Free variable number: % d \ n ", FN); 119 for (I = 0; I <var; I ++) 120 {121 If (free_x [I]) printf ("X % d rows are uncertain \ n", I + 1); 122 else printf ("X % d: % d \ n", I + 1, X [I]); 123} 124} 125 else126 {127 for (I = 0; I <var; I ++) 128 printf ("X % d: % d \ n ", I + 1, x [I]); 129} 130 cout <Endl; 131} 132 return 0; 133}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.