C, C++ Language/๐Ÿฅ‰ BOJ

(C++) โ˜…Number Theory Upper-Beginner I - 3 Solvedโ˜…

metamong 2024. 11. 14.

โ˜… 9506 ์•ฝ์ˆ˜๋“ค์˜ ํ•ฉ โ˜…

//9506
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,total;

	while(1){
        cin >> n;
        if(n==-1){
            break;
        }
        total = 1;
        vector<int> numbers = {};

        for(int x=2;x<n;x++){
            if(n%x==0){
                total+=x;
                numbers.push_back(x);
            }
        }

        if(total==n){
            sort(numbers.begin(), numbers.end());
            cout << n << " = 1";
            for(int num : numbers){
                cout << " + " << num;
            }
            cout << endl;
        }
        else cout << n << " is NOT perfect." << endl;

    }
	return 0;
}

 

๐Ÿงš‍โ™‚๏ธ ๊ฐ€๋ณ€ ๋ฐฐ์—ด vector<int> numbers = {} ๋งŒ๋“ค์–ด ๋†“๊ณ , ์•ฝ์ˆ˜์ผ ๋•Œ numbers.push_back(x); ์‚ฌ์šฉ.

 

๐Ÿงš‍โ™‚๏ธ ํ•œ ๊ฐœ ์ž…๋ ฅ ๋‹น ๋ฐ”๋กœ ํ•œ ๊ฐœ ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ cout << endl; ์ ์ ˆํžˆ ๋ฐฐ์น˜ ํ•„์š”


โ˜… 11653 ์†Œ์ธ์ˆ˜๋ถ„ํ•ด โ˜…

//11653
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

bool is_prime(int n){
    for(int x=2;x<=sqrt(n);x++){
        if(n%x==0){
            return false;
        }
    }
    return true;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N;
    cin >> N;

    vector<int> divisors = {};
    vector<int> prime_factors = {};
    vector<int> answers = {};

    if(N==1){
        return 0;
    }

    for(int n=2;n<=N;n++){
        if(N%n==0){
            divisors.push_back(n);
        }
    }

    for(int divisor : divisors){
        if(is_prime(divisor)){
            prime_factors.push_back(divisor);
        }
    }

    for(int prime_factor : prime_factors){
        while(N%prime_factor==0){
            N/=prime_factor;
            answers.push_back(prime_factor);
        }
        if(N==1) break;
    }

    sort(answers.begin(),answers.end());
    for(int answer:answers){
        cout << answer << '\n';
    }
    
	return 0;
}

 

๐Ÿงš‍โ™‚๏ธ ์†Œ์ธ์ˆ˜๋ถ„ํ•ด์˜ ์ •์˜์— ๋”ฐ๋ผ ์•ฝ์ˆ˜ ๋ฐฐ์—ด / ์†Œ์ธ์ˆ˜ ์ข…๋ฅ˜ ๋ฐฐ์—ด / ๊ทธ๋ฆฌ๊ณ  ์ •๋‹ต์ด ๋“ค์–ด๊ฐ„ ์†Œ์ธ์ˆ˜ ๋ฐฐ์—ด๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ๊ตฌํ•˜๋ฉด์„œ ์ง„ํ–‰.

 

๐Ÿงš‍โ™‚๏ธsort(), push_back() ์ ์ ˆํžˆ ์‚ฌ์šฉ

 

๐Ÿงš‍โ™‚๏ธ ์†Œ์ˆ˜ ํŒ์ •์€ #include <cmath> ์‚ฌ์šฉํ•ด์„œ sqrt(n)(ํฌํ•จ)๊นŒ์ง€ ์ง„ํ–‰


โ˜… 1934 ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ โ˜…

//1934
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
using namespace std;

int get_GCD(int a, int b){
    while(b!=0){
        int tmp;
        tmp = a;
        a = b;
        b = tmp % b;
    }
    return a;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int T, A, B, gcd;
    cin >> T;

    while(T--){
        cin >> A >> B;
        if(A>=B){
            gcd = get_GCD(A,B);
        }
        else{
            gcd = get_GCD(B,A);
        }
        cout << (A * B) / gcd << endl;
    }
	return 0;
}

 

๐Ÿงš‍โ™‚๏ธ ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•(ํฐ ์ˆ˜๋Š” ์ž‘์€ ์ˆ˜๋กœ, ์ž‘์€ ์ˆ˜๋Š” ํฐ ์ˆ˜๋ฅผ ์ž‘์€ ์ˆ˜๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ / ๊ทธ๋ฆฌ๊ณ  ์ž‘์€ ์ˆ˜๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋Œ๋ฆฌ๊ธฐ)์œผ๋กœ ๋‘ ์ˆ˜์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ , A x B = G x L ๊ณต์‹์„ ํ™œ์šฉํ•ด ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ L์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.


 

 

 

 

 

 

 

 

 

 

๋Œ“๊ธ€