Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void insertionSort(double arr[], int n)
  4. {
  5.     for (int i = 1; i < n; i++)
  6.     {
  7.         double current = arr[i];
  8.         int j = i;
  9.         while(--j >= 0 && arr[j] > current)
  10.         {
  11.             arr[j + 1] = arr[j];
  12.         }
  13.         arr[j + 1] = current;
  14.     }
  15. }
  16.  
  17. using namespace std;
  18.  
  19. int main ()
  20. {
  21.     double price, arr[100];
  22.     cin >> price;
  23.     int n;
  24.     cin >> n;
  25.  
  26.     for (int i = 0; i < n; i++)
  27.     {
  28.         cin >> arr[i];
  29.     }
  30.  
  31.     insertionSort(arr, n);
  32.  
  33.     double currentSum = 0;
  34.     bool canPurchase = false;
  35.  
  36.     for (int i = n - 1; i >= 0; i--)
  37.     {
  38.         double nextSum = arr[i] + currentSum;
  39.         if(nextSum == price)
  40.         {
  41.             canPurchase = true;
  42.             break;        
  43.         }
  44.         else if(nextSum < price)
  45.         {
  46.             currentSum += arr[i];
  47.         }
  48.     }
  49.  
  50.     cout << canPurchase << endl;
  51.  
  52.     return 0;
  53. }
  54.  
  55.  
  56.  
  57.  
  58. #include <iostream>
  59.  
  60. using namespace std;
  61.  
  62. const int matrixSize = 9;
  63.  
  64. /*
  65. int sudoku[matrixSize][matrixSize] = {
  66.     {5, 3, 4, 6, 7, 8, 9, 1, 2},
  67.     {6, 7, 2, 1, 9, 5, 3, 4, 8},
  68.     {1, 9, 8, 3, 4, 2, 5, 6, 7},
  69.     {8, 5, 9, 7, 6, 1, 4, 2, 3},
  70.     {4, 2, 6, 8, 5, 3, 7, 9, 1},
  71.     {7, 1, 3, 9, 2, 4, 8, 5, 6},
  72.     {9, 6, 1, 5, 3, 7, 2, 8, 4},
  73.     {2, 8, 7, 4, 1, 9, 6, 3, 5},
  74.     {3, 4, 5, 2, 8, 6, 1, 7, 9},
  75. };
  76. */
  77.  
  78. int sudoku[matrixSize][matrixSize] = {
  79.     {5, 3, 0, 0, 7, 0, 0, 0, 0},
  80.     {6, 0, 0, 1, 9, 5, 0, 0, 0},
  81.     {0, 9, 8, 0, 0, 0, 0, 6, 0},
  82.     {8, 0, 0, 0, 6, 0, 0, 0, 3},
  83.     {4, 0, 0, 8, 0, 3, 0, 0, 1},
  84.     {7, 0, 0, 0, 2, 0, 0, 0, 6},
  85.     {0, 6, 0, 0, 0, 0, 2, 8, 0},
  86.     {0, 0, 0, 4, 1, 9, 0, 0, 5},
  87.     {0, 0, 0, 0, 8, 0, 0, 7, 9},
  88. };
  89.  
  90. void resetValues(bool expectedValues[])
  91. {
  92.     for (int i = 0; i < 9; i++)
  93.     {
  94.         expectedValues[i] = false;
  95.     }
  96. }
  97.  
  98. bool areRowsValid(bool expectedValues[])
  99. {
  100.     for (int i = 0; i < matrixSize; i++)
  101.     {
  102.         for (int j = 0; j < matrixSize; j++)
  103.         {
  104.             int number = sudoku[i][j];
  105.             if (expectedValues[number - 1])
  106.             {
  107.                 return false;
  108.             }
  109.  
  110.             expectedValues[number - 1] = true;
  111.         }
  112.  
  113.         resetValues(expectedValues);
  114.     }
  115. }
  116.  
  117. bool areColsValid(bool expectedValues[])
  118. {
  119.     for (int i = 0; i < matrixSize; i++)
  120.     {
  121.         for (int j = 0; j < matrixSize; j++)
  122.         {
  123.             int number = sudoku[j][i];
  124.             if (expectedValues[number - 1])
  125.             {
  126.                 return false;
  127.             }
  128.  
  129.             expectedValues[number - 1] = true;
  130.         }
  131.  
  132.         resetValues(expectedValues);
  133.     }
  134. }
  135.  
  136. bool areSquaresValid(bool expectedValues[])
  137. {
  138.     for (int i = 0; i < matrixSize; i += 3)
  139.     {
  140.         for (int j = 0; j < matrixSize; j += 3)
  141.         {
  142.             for (int k = 0; k < 3; k++)
  143.             {
  144.                 for (int l = 0; l < 3; l++)
  145.                 {
  146.                     int number = sudoku[i + k][j + l];
  147.                     if (expectedValues[number - 1])
  148.                     {
  149.                         return false;
  150.                     }
  151.  
  152.                     expectedValues[number - 1] = true;
  153.                 }
  154.             }
  155.  
  156.             resetValues(expectedValues);
  157.         }
  158.     }
  159. }
  160.  
  161. bool isCompleted()
  162. {
  163.     bool expectedValues[9] = {false, false, false, false, false, false, false, false, false};
  164.  
  165.     return areRowsValid(expectedValues) && areColsValid(expectedValues) && areSquaresValid(expectedValues);
  166. }
  167.  
  168. int main()
  169. {
  170.  
  171.     cout << isCompleted() << endl;
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement