Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- void insertionSort(double arr[], int n)
- {
- for (int i = 1; i < n; i++)
- {
- double current = arr[i];
- int j = i;
- while(--j >= 0 && arr[j] > current)
- {
- arr[j + 1] = arr[j];
- }
- arr[j + 1] = current;
- }
- }
- using namespace std;
- int main ()
- {
- double price, arr[100];
- cin >> price;
- int n;
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- cin >> arr[i];
- }
- insertionSort(arr, n);
- double currentSum = 0;
- bool canPurchase = false;
- for (int i = n - 1; i >= 0; i--)
- {
- double nextSum = arr[i] + currentSum;
- if(nextSum == price)
- {
- canPurchase = true;
- break;
- }
- else if(nextSum < price)
- {
- currentSum += arr[i];
- }
- }
- cout << canPurchase << endl;
- return 0;
- }
- #include <iostream>
- using namespace std;
- const int matrixSize = 9;
- /*
- int sudoku[matrixSize][matrixSize] = {
- {5, 3, 4, 6, 7, 8, 9, 1, 2},
- {6, 7, 2, 1, 9, 5, 3, 4, 8},
- {1, 9, 8, 3, 4, 2, 5, 6, 7},
- {8, 5, 9, 7, 6, 1, 4, 2, 3},
- {4, 2, 6, 8, 5, 3, 7, 9, 1},
- {7, 1, 3, 9, 2, 4, 8, 5, 6},
- {9, 6, 1, 5, 3, 7, 2, 8, 4},
- {2, 8, 7, 4, 1, 9, 6, 3, 5},
- {3, 4, 5, 2, 8, 6, 1, 7, 9},
- };
- */
- int sudoku[matrixSize][matrixSize] = {
- {5, 3, 0, 0, 7, 0, 0, 0, 0},
- {6, 0, 0, 1, 9, 5, 0, 0, 0},
- {0, 9, 8, 0, 0, 0, 0, 6, 0},
- {8, 0, 0, 0, 6, 0, 0, 0, 3},
- {4, 0, 0, 8, 0, 3, 0, 0, 1},
- {7, 0, 0, 0, 2, 0, 0, 0, 6},
- {0, 6, 0, 0, 0, 0, 2, 8, 0},
- {0, 0, 0, 4, 1, 9, 0, 0, 5},
- {0, 0, 0, 0, 8, 0, 0, 7, 9},
- };
- void resetValues(bool expectedValues[])
- {
- for (int i = 0; i < 9; i++)
- {
- expectedValues[i] = false;
- }
- }
- bool areRowsValid(bool expectedValues[])
- {
- for (int i = 0; i < matrixSize; i++)
- {
- for (int j = 0; j < matrixSize; j++)
- {
- int number = sudoku[i][j];
- if (expectedValues[number - 1])
- {
- return false;
- }
- expectedValues[number - 1] = true;
- }
- resetValues(expectedValues);
- }
- }
- bool areColsValid(bool expectedValues[])
- {
- for (int i = 0; i < matrixSize; i++)
- {
- for (int j = 0; j < matrixSize; j++)
- {
- int number = sudoku[j][i];
- if (expectedValues[number - 1])
- {
- return false;
- }
- expectedValues[number - 1] = true;
- }
- resetValues(expectedValues);
- }
- }
- bool areSquaresValid(bool expectedValues[])
- {
- for (int i = 0; i < matrixSize; i += 3)
- {
- for (int j = 0; j < matrixSize; j += 3)
- {
- for (int k = 0; k < 3; k++)
- {
- for (int l = 0; l < 3; l++)
- {
- int number = sudoku[i + k][j + l];
- if (expectedValues[number - 1])
- {
- return false;
- }
- expectedValues[number - 1] = true;
- }
- }
- resetValues(expectedValues);
- }
- }
- }
- bool isCompleted()
- {
- bool expectedValues[9] = {false, false, false, false, false, false, false, false, false};
- return areRowsValid(expectedValues) && areColsValid(expectedValues) && areSquaresValid(expectedValues);
- }
- int main()
- {
- cout << isCompleted() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement