Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.85 KB | None | 0 0
  1. ///C# shit
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Курсовая_часть_3
  9. {
  10.     /*TODO
  11.      * НАХОЖДЕНИЕ ПАРОСОЧЕТАНИЙ
  12.      * МАКСИМАЛЬНОЕ ПАРОСОЧЕТАНИЕ
  13.      * */
  14.  
  15.      //
  16.     /**gotta get a grip
  17.     to find permanent we need a matrix like this:
  18.  
  19.     1 0 0 0 0 0
  20.     0 1 1 0 0 0
  21.     0 1 1 0 0 0
  22.     0 1 1 0 0 0
  23.     0 1 1 0 0 0
  24.     1 0 1 1 1 1
  25.  
  26.     if we want to make a big matrix from this it will look like this:
  27.  
  28. 0 0 0 0 0 0|1 0 0 0 0 0
  29. 0 0 0 0 0 0|0 1 1 0 0 0
  30. 0 0 0 0 0 0|0 1 1 0 0 0
  31. 0 0 0 0 0 0|0 1 1 0 0 0
  32. 0 0 0 0 0 0|0 1 1 0 0 0
  33. 0 0 0 0 0 0|1 0 1 1 1 1
  34. -----------------------
  35. 1 0 0 0 0 1|0 0 0 0 0 0
  36. 0 1 1 1 1 0|0 0 0 0 0 0
  37. 0 1 1 1 1 0|0 0 0 0 0 0
  38. 0 0 0 0 0 1|0 0 0 0 0 0
  39. 0 0 0 0 0 1|0 0 0 0 0 0
  40. 0 0 0 0 0 1|0 0 0 0 0 0
  41.  
  42. right upper square is our part for finding permanent
  43. left down sqaure is the transponent right upper
  44. */
  45.     class Graph
  46.     {
  47.         int[,] Matrix;
  48.         //int[][] Matrix2;
  49.         int amount;
  50.         int sum;
  51.         const double EPS = 1E-9;
  52.         public Graph()
  53.         {
  54.             amount = 0;
  55.             Matrix = new int[0, 0];
  56.             sum = 0;
  57.         }
  58.         public int[,] CreateGraph()
  59.         {
  60.             Console.WriteLine("Enter the amount of vertexes");
  61.             amount = int.Parse(Console.ReadLine())/2;
  62.             Matrix = new int[amount, amount];
  63.             for (int i = 0; i < amount; i++)
  64.             {
  65.                 Console.WriteLine("Enter new row of matrix, separating values by space");
  66.                 string[] str = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  67.                 for (int j = 0; j < amount; j++)
  68.                 {
  69.                     int.TryParse(str[j], out Matrix[i, j]);
  70.                 }
  71.                 Console.WriteLine();
  72.             }
  73.             return Matrix;
  74.         }
  75.         public void Display()
  76.         {
  77.             Console.WriteLine("Matrix:");
  78.             for (int i = 0; i < amount; i++)
  79.             {
  80.                 for (int j = 0; j < amount; j++)
  81.                 {
  82.                     Console.Write("|" + Matrix[i, j] + "|");
  83.                 }
  84.                 Console.WriteLine();
  85.                 Console.WriteLine("-------------------------");
  86.             }
  87.             Console.WriteLine("Permanent is: {0}", permanent(Matrix));
  88.         }
  89.  
  90.         public static int[,] minor(int[,] A, int x, int y)
  91.         {
  92.             int length = A.GetLength(0) - 1;
  93.             //Console.WriteLine(length);
  94.             int[,] result = new int[length, length];
  95.             for (int i = 0; i < length; i++)
  96.             {
  97.                 for (int j = 0; j < length; j++)
  98.                 {
  99.                     if (i < x && j < y)
  100.                     {
  101.                         result[i, j] = A[i, j];
  102.                     }
  103.                     else if (i >= x && j < y)
  104.                     {
  105.                         result[i, j] = A[i + 1, j];
  106.                     }
  107.                     else if (i < x && j >= y)
  108.                     {
  109.                         result[i, j] = A[i, j + 1];
  110.                     }
  111.                     else
  112.                     {
  113.                         result[i, j] = A[i + 1, j + 1];
  114.                     }
  115.                 }
  116.             }
  117.             return result;
  118.         }
  119.  
  120.         public int permanent(int[,] A)
  121.         {
  122.             if(A.GetLength(0) == 1){
  123.                 return A[0,0];
  124.             }
  125.             else{
  126.  
  127.                 for (int i = 0; i < A.GetLength(0); i++)
  128.                 {
  129.                     for (int j = 0; j < A.GetLength(0); j++)
  130.                     {
  131.                         Console.Write("|" + A[i, j] + "|");
  132.                     }
  133.                     Console.WriteLine();
  134.                 }
  135.                 Console.WriteLine();
  136.                 for (int i =0; i<A.GetLength(0); i++){
  137.                     sum += A[0,i] * permanent(minor(A,0,i)); //A in brackets
  138.                 }
  139.             }
  140.             return sum;
  141.         }
  142.     }
  143.     //create class permanent or паросочетание
  144.     public static class ArrayExtensions
  145.     {
  146.         public static T[] GetRow<T>(this T[,] data, int i)
  147.         {
  148.             T[] row = new T[data.GetLength(1)];
  149.             //T[] row = new T[data.GetLength(i)];
  150.             for (int j = 0; j < /*data.GetLength(i)*/ row.Length; j++)
  151.             {
  152.                 row[j] = data[i, j];
  153.             }
  154.             return row;
  155.         }
  156.     }
  157.     class Program
  158.     {
  159.         static void Main(string[] args)
  160.         {
  161.             Graph dvydol = new Graph();
  162.             dvydol.CreateGraph();
  163.             dvydol.Display();
  164.             Console.ReadKey();
  165.         }
  166.     }
  167. }
  168.  
  169.  
  170. ///Java Rosetta
  171. import java.util.Scanner;
  172.  
  173. public class MatrixArithmetic {
  174.     public static double[][] minor(double[][] a, int x, int y){
  175.         int length = a.length-1;
  176.         double[][] result = new double[length][length];
  177.         for(int i=0;i<length;i++) for(int j=0;j<length;j++){
  178.             if(i<x && j<y){
  179.                 result[i][j] = a[i][j];
  180.             }else if(i>=x && j<y){
  181.                 result[i][j] = a[i+1][j];
  182.             }else if(i<x && j>=y){
  183.                 result[i][j] = a[i][j+1];
  184.             }else{ //i>x && j>y
  185.                 result[i][j] = a[i+1][j+1];
  186.             }
  187.         }
  188.         return result;
  189.     }
  190.    
  191.     public static double perm(double[][] a){
  192.         if(a.length == 1){
  193.             return a[0][0];
  194.         }else{
  195.             double sum = 0;
  196.             for(int i=0;i<a.length;i++){
  197.                 sum += a[0][i] * perm(minor(a,0,i));
  198.             }
  199.             return sum;
  200.         }
  201.     }
  202.     public static void main(String args[]){
  203.         Scanner sc = new Scanner(System.in);
  204.         int size = sc.nextInt();
  205.         double[][] a = new double[size][size];
  206.         for(int i=0;i<size;i++) for(int j=0;j<size;j++){
  207.             a[i][j] = sc.nextDouble();
  208.         }
  209.         sc.close();
  210.         System.out.println("Determinant: "+det(a));
  211.         System.out.println("Permanent: "+perm(a));
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement