โ 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์ ์ถ๋ ฅํ๋ฉด ๋๋ค.
'C, C++ Language > ๐ฅ BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| (C++)โ Sorting Upper-Beginner I - 1 Solvedโ (1) | 2024.11.14 |
|---|---|
| (C++)โ Math & Geometry Upper-Beginner I - 1 Solvedโ (0) | 2024.11.14 |
| (C++) โ Implementation Upper-Beginner I - 8 Solvedโ (0) | 2024.11.13 |
| (C++) โ Implementation Beginner I - 28 Solvedโ (0) | 2024.11.13 |
| (C++) โ Basics I - 16 Solvedโ (0) | 2024.11.13 |
๋๊ธ