fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. struct info
  6. {
  7. double eqn[100];
  8. double res;
  9. } matrix[100];
  10.  
  11. bool no_soln = false;
  12.  
  13. void forming_upper_trianguler_matrix(int dim)
  14. {
  15. for(int i = 1; i <= dim-1; i++)
  16. {
  17. for(int j = i + 1; j <= dim; j++)
  18. {
  19. double multiple = matrix[j].eqn[i] / matrix[i].eqn[i];
  20.  
  21. if(matrix[i].eqn[i] == 0)
  22. {
  23. no_soln = true;
  24. break;
  25. }
  26.  
  27. for(int k = 1; k <= dim; k++)
  28. {
  29. matrix[j].eqn[k] = matrix[j].eqn[k] - matrix[i].eqn[k] * multiple;
  30. }
  31. matrix[j].res = matrix[j].res - matrix[i].res*multiple;
  32. }
  33. }
  34. }
  35.  
  36. void lower_trianguler_matrix(int dim)
  37. {
  38. for(int i = dim; i > 1 ; i--)
  39. {
  40. for(int j = i-1; j>= 1; j--)
  41. {
  42. double multiple = matrix[j].eqn[i] / matrix[i].eqn[i];
  43. if(matrix[i].eqn[i] == 0)
  44. {
  45. no_soln = true;
  46. break;
  47. }
  48. for(int k = dim; k >= 1; k--)
  49. {
  50. matrix[j].eqn[k] = matrix[j].eqn[k] - matrix[i].eqn[k] * multiple;
  51. }
  52. matrix[j].res = matrix[j].res - matrix[i].res * multiple;
  53. }
  54. }
  55. }
  56.  
  57.  
  58.  
  59. int main(void)
  60. {
  61. int dim;
  62. scanf("%d",&dim); //dimention of the matrix
  63. for(int i = 1; i <= dim; i++) //dimention if n there will be n eqn and n variables per eqn
  64. {
  65. for(int j = 1; j <= dim; j++)
  66. {
  67. scanf("%lf",&matrix[i].eqn[j]);
  68. }
  69. scanf("%lf",&matrix[i].res);
  70. }
  71. forming_upper_trianguler_matrix(dim);
  72. if(no_soln == true)
  73. {
  74. printf("There is no unique solution.\n");
  75. }
  76. else
  77. {
  78. lower_trianguler_matrix(dim);
  79. if(no_soln == true)
  80. {
  81. printf("There is no unique solution.\n");
  82. }
  83. else
  84. {
  85. for(int i = 1; i <= dim; i++)
  86. {
  87. printf("solution of variable %d = %lf\n",i,matrix[i].res/matrix[i].eqn[i]);
  88. }
  89. }
  90.  
  91. }
  92. return 0;
  93.  
  94. }
  95.  
Time limit exceeded #stdin #stdout 5s 3544KB
stdin
Standard input is empty
stdout
Standard output is empty