C++ : 수학 관련 함수

Se0ng_1l·2022년 12월 5일
0

코딩기법

목록 보기
4/6
post-thumbnail

🧮 수학 관련 함수

1. 제곱, pow()

#include <iostream>
#include <cmath>

using namespace std;

int main(){
    int a = 4;
    cout << pow(a, 2) << endl;
    // 결과: 16
}

2.제곱근, sqrt()

#include <iostream>
#include <cmath>

using namespace std;

int main(){
    int a = 4;
    cout << sqrt(a) << endl;
    // 결과: 2
}

3.약수의 개수 구하기

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int num = 15; // 15의 약수의 개수 구하기
    int cnt = 0;
    for(int i = 1; i * i <= num; i++)
    {
        if (num % i == 0) {
            cnt++;
            if (i * i < num)
                cnt++;
        }
    }
    cout << cnt << endl;
}
profile
치타가 되고 싶은 취준생

0개의 댓글