Skip to content

Instantly share code, notes, and snippets.

@komkanit
Created November 30, 2015 03:09
Show Gist options
  • Save komkanit/5bbb44b4866fa075e91d to your computer and use it in GitHub Desktop.
Save komkanit/5bbb44b4866fa075e91d to your computer and use it in GitHub Desktop.
using System;
class Gauss
{
static double[,] matrix = new double[10,10];
static int n;
public static void Main()
{
n = int.Parse(Console.ReadLine());
double[] ans = new double[n];
Input();
GaussElim();
//Print();
//Cal(ans);
//PrintAnsElim(ans);
Jodan();
PrintAnsJondan();
}
static void PrintAnsElim(double[] ans)
{
for(int i=0 ; i<n ; i++)
Console.WriteLine("{0}",ans[i]);
}
static void PrintAnsJondan()
{
for(int i=0 ; i<n ; i++)
{
Console.WriteLine("{0}",matrix[i,n]);
}
}
static void Input()
{
for(int i=0 ; i<n ; i++)
{
string str = Console.ReadLine();
string[] s = str.Split(' ');
for(int j=0 ; j<n+1 ; j++)
{
matrix[i,j] = double.Parse(s[j]);
}
}
}
static void Print()
{
for(int i=0 ;i<n ; i++)
{
for(int j=0 ; j<n+1 ; j++)
{
Console.Write("{0} ",matrix[i,j]);
}
Console.WriteLine();
}
}
static void GaussElim()
{
int i,j,k;
double a,b;
for(j=0 ; j<n-1 ; j++)
{
for(i=j+1 ; i<n ; i++)
{
a = matrix[i,j];
b = matrix[j,j];
for(k=j ; k<n+1 ; k++)
{
matrix[i,k] -= matrix[j,k]*a/b;
//Console.WriteLine("**");
}
}
}
}
static void Jodan()
{
int i,j,c;
double a;
for(i = n-1,c = n-1; i>=0 ; i--,c--)
{
for(j = n-1 ; j>=c ; j--)
{
if(j == c)
{
matrix[i,n]/=matrix[i,j];
matrix[i,j]/=matrix[i,j];
}
else
{
a = matrix[i,j];
matrix[i,j]-=a*matrix[j,j];
matrix[i,n]-=a*matrix[j,n];
}
}
}
}
static void Cal(double[] ans)
{
int i,j;
for(i = n-1 ; i>=0 ; i--)
{
for(j = n-1 ; j>=i ; j--)
{
if(j == i)
{
ans[i] = matrix[i,n]/matrix[i,j];
}
else
{
matrix[i,n] -= matrix[i,j]*ans[j];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment