Advertisement
Guest User

Untitled

a guest
Oct 18th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <vector>
  2. #include <cmath>
  3. #include <ctype.h>
  4. #include <cassert>
  5. #include <ctime>
  6. #include <climits>
  7. #include <limits>
  8. #include <algorithm>
  9. #include <list>
  10. #include <set>
  11. #include <map>
  12. #include <string>
  13. #include <stdio.h>
  14. #include <queue>
  15. #include <stack>
  16. #include <iomanip>
  17. #include <bitset>
  18. #include <utility>
  19. #include <deque>
  20. #include <stdlib.h>
  21. #include <functional>
  22. #include <float.h>
  23. //#define C11
  24. //#include <windows.h>
  25.  
  26. #ifdef C11
  27.     #include <unordered_map>
  28.     #include <unordered_set>
  29.     #include <tuple>
  30. #endif // C11
  31.  
  32. #define F first
  33. #define S second
  34. #define mp make_pair
  35. #define pb push_back
  36. #define fabs(x) ((x>0) ? (x) : -1*(x))
  37. #define show(a,n) cout <<#a<<": "; for (int iii=0;iii<n;iii++) cout <<a[iii]<<" "; cout<<"\n";
  38. #define show2(a,n,m) cout <<#a<<":\n"; for (int iii=0;iii<n;iii++) { for(int jjj=0;jjj<m;jjj++) cout <<a[iii][jjj]<<" "; cout <<"\n";}
  39. #define name(x) cout <<#x<<" \n";
  40. #define print(x) cout <<#x"="<<x<<"\n";
  41. #define letters char alp[30]={qwertyuiopasdfghjklzxcvbnm},sogl[30]={qwrtpsdfghjklzxcvbnm},gl[30]={eyuioa};
  42. #define SetBit(value,place) (value|(1<<place))
  43. #define ClearBit(value,place) (value&(~(1<<place)))
  44. #define InverseBit(value,place) (value^(1<<place))
  45. #define StartClock time_t inittime=clock();
  46. #define GetClock fprintf(stderr,"Time: %f\n",1.0*(clock()-inittime)/CLOCKS_PER_SEC);
  47.  
  48. typedef std::pair<int,int> pii;
  49. typedef std::vector <int>::iterator iti;
  50. typedef std::multiset <int>::iterator itm;
  51. typedef std::set <int>::iterator itset;
  52. typedef std::string::iterator its;
  53. typedef std::pair<long long,long long> pll;
  54. typedef std::vector <std::vector <int> > graph;
  55. typedef unsigned long long ull;
  56. typedef unsigned int ui;
  57.  
  58. using namespace std;
  59.  
  60. const int INF=INT_MAX;
  61. const long long LINF=LLONG_MAX;
  62. const unsigned long long ULINF=ULLONG_MAX;
  63. const double DINF=DBL_MAX;
  64. const double EPS=0.000001;
  65. const int mod=1000000007;
  66.  
  67. #define ONLINE_JUDGE
  68.  
  69. #ifndef ONLINE_JUDGE
  70.     #include <fstream>
  71.     ifstream cin("input.txt");
  72.     ofstream cout("output.txt");
  73. #else
  74.     #include <iostream>
  75. #endif // LOCAL
  76.  
  77. int main()
  78. {
  79.     ios_base::sync_with_stdio(0); //cin.tie(0);
  80.     int n;
  81.     cout <<"Enter n for n x n matrix: ";
  82.     cin >>n;
  83.     if (n > 10)
  84.     {
  85.         cout <<"n is too big, must be <=10\n";
  86.         return 0;
  87.     }
  88.     else if (n <= 0)
  89.     {
  90.         cout <<"n is less than 1\n";
  91.         return 0;
  92.     }
  93.     double** v = new double*[n];
  94.     for (int i=0;i<n;i++)
  95.         v[i] = new double[n];
  96.     cout <<"Enter upper triangle of the matrix:\n";
  97.     for (int i=0;i<n;i++)
  98.         for (int j=0;j<i+1;j++)
  99.         {
  100.             cin >>v[i][j];
  101.             v[j][i]=v[i][j];
  102.         }
  103.     cout <<"So given matrix is:\n";
  104.     for (int i=0;i<n;i++)
  105.     {
  106.         for (int j=0;j<n;j++)
  107.         {
  108.             cout.width(11);
  109.             cout <<fixed<<setprecision(6)<<v[i][j];
  110.         }
  111.         cout <<"\n";
  112.     }
  113.     for (int i=0;i<n;i++)
  114.     {
  115.         double prev_max = (i==0 ? DINF : v[i-1][i-1]);
  116.         pii maxi=mp(n-1,n-1);
  117.         for (int j=0;j<n;j++)
  118.         {
  119.             for (int k=0;k<n;k++)
  120.             {
  121.                 if (v[j][k] > (v[maxi.F][maxi.S] + EPS) && !(v[j][k] > prev_max + EPS) && !(j == k && k < i))
  122.                 {
  123.                     maxi=mp(j,k);
  124.                 }
  125.             }
  126.         }
  127.         swap(v[i][i],v[maxi.F][maxi.S]);
  128.     }
  129.     cout <<"Matrix after transformation:\n";
  130.     for (int i=0;i<n;i++)
  131.     {
  132.         for (int j=0;j<n;j++)
  133.         {
  134.             cout.width(11);
  135.             cout <<fixed<<setprecision(6)<<v[i][j];
  136.         }
  137.         cout <<"\n";
  138.     }
  139.     int index = -1;
  140.     for (int i=0;i<n;i++)
  141.     {
  142.         bool excpt = false;
  143.         for (int j=0;j<n;j++)
  144.         {
  145.             if (v[i][j] >= EPS)
  146.             {
  147.                 excpt = true;
  148.                 break;
  149.             }
  150.         }
  151.         if (!excpt)
  152.         {
  153.             index = i;
  154.             break;
  155.         }
  156.     }
  157.     if (index == -1)
  158.         cout <<"There is no row with only negative elements!\n";
  159.     else
  160.         cout <<"The first row of only negative elements is "<<index<<"\n";
  161.     for (int i=0;i<n;i++)
  162.         delete [] v[i];
  163.     delete [] v;
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement