Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string>
- using namespace std;
- void sortDesc(int* niz,int arraySize);
- void ispisNiza(int* niz, int arraySize);
- int hittingTheTarget(int* niz, int arraySize, int position, int target);
- int main(void) {
- int* niz = NULL;
- int arraySize = 0;
- int target = 0;
- int position = 0;
- int counter = -1;
- int min = INT_MAX;
- cout << "Koliko ce velik niz bit: ";
- cin >> arraySize;
- niz = new int[arraySize];
- cout << "\nUnesi brojeve u tom nizu: ";
- for (int i = 0;i < arraySize;i++) {
- cin >> niz[i];
- }
- sortDesc(niz, arraySize);
- cout << "Zeljeni zbroj: ";
- cin >> target;
- for (int i = 0;i < arraySize;i++) {
- counter=hittingTheTarget(niz, arraySize, i, target);
- if (min > counter && counter!=-1)
- min = counter;
- }
- if (counter == -1)
- cout << "You done fucked it";
- else
- cout << "\nKolicina je: " << min << endl;
- return 0;
- }
- void sortDesc(int *niz,int arraySize) {
- int i = 0;
- int j = 0;
- int temp = 0;
- for (i = 0;i < arraySize;i++) {
- for (j = i + 1;j < arraySize;j++) {
- if (niz[i] < niz[j]) {
- temp = niz[i];
- niz[i] = niz[j];
- niz[j] = temp;
- }
- }
- }
- }
- void ispisNiza(int* niz,int arraySize) {
- for (int i = 0;i < arraySize;i++) {
- cout << niz[i] << " ";
- }
- }
- int hittingTheTarget(int* niz, int arraySize, int position, int target) {
- int i = 0;
- int result = 0;
- int counter = 0;
- for (i=position;i < arraySize;i++)
- {
- result += niz[i];
- counter++;
- if (result == target) {
- return counter;
- }
- else if (result > target)
- {
- result -= niz[i];
- counter--;
- }
- else
- i--;
- }
- return -1;
- }
Add Comment
Please, Sign In to add comment