Created
April 16, 2022 15:45
-
-
Save Park-Developer/0becb26d319cecd9f2cd5b3596530cfb to your computer and use it in GitHub Desktop.
binary_conversion_func
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <queue> | |
#include <list> | |
#include <vector> | |
#include <algorithm> | |
#include <tuple> | |
#include <cmath> | |
using namespace std; | |
vector<int> conversion(int bit_size, int num_toC){ | |
vector<int> v; | |
for(int t=0;t<bit_size;t++){ | |
v.push_back(1); | |
} | |
int bit; | |
int div; | |
int idx=0; | |
while(num_toC!=0){ | |
bit=num_toC%2; | |
// 변환작업 Begin | |
// 0이면 +, 1이면 -1로 변환 | |
if(bit==0){ | |
bit=1; | |
}else if(bit==1){ | |
bit=-1; | |
} | |
// 변환작업 End | |
v[idx]=bit; | |
num_toC=num_toC/2; | |
idx++; | |
} | |
reverse(v.begin(), v.end()); | |
return v; | |
} | |
int solution(vector<int> numbers, int target) { | |
int answer = 0; | |
int case_num=pow(2,numbers.size()); | |
vector<int> temp; | |
for(int n=0;n<case_num;n++){ | |
temp=conversion(numbers.size(),n); | |
int sum=0; | |
//cout << "CASE : " << n ; | |
for(int tt=0; tt<numbers.size(); tt++){ | |
//cout << numbers[tt]*temp[tt]<< " "; | |
sum+=(numbers[tt]*temp[tt]); | |
} | |
//cout << endl; | |
//cout << "sum " <<sum<< endl; | |
if(sum==target){ | |
// <target 조합 확인> | |
//cout<< "CASE : "<<n << ", sum : " << sum << endl; | |
answer++; | |
//cout << "["; | |
//for(int tt=0; tt<numbers.size(); tt++){ | |
//cout << numbers[tt]*temp[tt] <<", " ; | |
//} | |
//cout << "]" << endl; | |
} | |
} | |
return answer; | |
} | |
int main(){ | |
vector<int> numbers={4, 1, 2, 1}; | |
int target =4; | |
cout << "RESULT" << endl; | |
cout << solution(numbers, target) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment