Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- 주가예측
- Python
- java
- Reflex
- frontend
- 코스닥
- 빅데이터
- 자바
- 코딩테스트
- java정리
- 개발자
- Project
- 프로그래밍
- SNS
- 코딩독학
- 개인프로젝트
- 이웃한 칸
- backend
- 프로그래머스
- 백준
- 코테
- 개발
- 코딩
- C++
- 모의 주식
- 사물인터넷
- 프로그램
- 자바독학
- 풀스택프레임워크
- 자바정리
Archives
- Today
- Total
Creative Code
[C++]소수로 만들 수 있는 가장 긴 합성수 수열 본문
//N번째 소수까지 사용해 길이가 가장 긴 연속된 합성수 수열을 만들때 수열의 최소 소인수 배열
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int>primes(int &N) {
vector<int>prime;
prime.push_back(2);
prime.push_back(3);
for (int i = 4; i < 10000; i++) {
int count = 0;
for (int j = 2; j < i; j++) {
if (i % j != 0) {
count++;
}
}
if (count == i - 2) {
prime.push_back(i);
}
if (prime.size() == N) {
break;
}
}
return prime;
}
int solution(int N) {
vector<int>prime = primes(N);
long long index = 4;
vector<long long>num;
int temp = 0;
long long fac=1;
for (int i = 0; i < prime.size(); i++) {
fac *= prime[i];
}
while (index > 3 && index<=fac) {
int cnt = 0;
for (int i = 0; i < prime.size();) {
if (index % prime[i] == 0) {
num.push_back((long long)prime[i]);
break;
}
else {
i++;
}
if (i == prime.size()) {
int number = num.size();
if (temp <= number) {
temp = number;
for (int j = 0; j < num.size(); j++) {
cout << num[j] << " ";
}
cout << "/// " << number/*배열의 길이*/ << "(" << index - number/*배열의 첫 숫자*/ << ")";
cout << endl;
}
num.clear();
}
}
index++;
}
return 1;
}
int main() {
cout << solution(11) << endl; // 소수를 11개 사용해 만들 수 있는 가장 긴 합성수 수열
}
'혼자 만든 Code' 카테고리의 다른 글
[Python]명함 관리 프로그램 (0) | 2023.10.16 |
---|---|
[Python]명함 관리 프로그램 (0) | 2023.10.16 |
[C++]텍스트 RPG게임 (0) | 2023.08.28 |
[C]축구선수 관리 프로그램 (0) | 2023.08.28 |
[C]디데이 계산기 (0) | 2023.08.28 |