거듭제곱, 제곱근
- cmath 라이브러리 추가
pow(a, n) , sqrt(n)
사용
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector<int> v;
bool compare(int a, int b){
if(a > b){
return true;
}else{
return false;
}
}
long long solution(long long n) {
long long answer = 0;
string s = to_string(n);
for(int i=0; i<s.size(); i++){
v.push_back(int(s[i]-'0'));
}
sort(v.begin(), v.end(), compare);
for(int i=0; i<v.size(); i++){
int end = v.size() -1 - i;
answer += v[end] * pow(10, i) ;
}
return answer;
}
최대공약수, 최소공배수
- 최대공약수와 최소공배수 (프로그래머스 LV1)
#include <string>
#include <vector>
using namespace std;
int uclid(int a, int b){
while(a % b != 0){
int mod = a % b;
a = b;
b = mod;
}
return b;
}
int comm (int a, int b){
int g = uclid(a, b);
if(g == 1){
return a * b;
}
a /= g;
b /= g;
return a * b * g;
}
vector<int> solution(int n, int m) {
vector<int> answer;
answer.push_back(uclid(n, m));
answer.push_back(comm(n, m));
return answer;
}