Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[정수론] 9월 2일 #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 03_정수론/1735.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <algorithm>

using namespace std;

int gcdIter(int x, int y){//y>x
while(x){
y%=x;
swap(x,y);
}
return y;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반복문을 사용해 유클리드 호제법을 구현해주셨네요! 잊지 않고 swap해주시는 것까지 완벽합니다😎

}
int main()
{
//변수 선언
int a, b, c, d, x, y, gcd;

//입력
cin>>a>>b;
cin>>c>>d;

x=a*d+b*c;
y=b*d;


gcd=gcdIter(x, y);

x/=gcd;
y/=gcd;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

통분하고 약분해주는 과정도 깔끔하네요😍


//출력
cout<<x<<" "<<y;

return 0;
}
62 changes: 62 additions & 0 deletions 03_정수론/2840.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct wheel{
int n;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. 스네이크 표기법을 활용하여 main 함수의 N과 K를 소문자 n, k로 정의하게 되면 여기서의 n과 겹치게 되겠네요! 다른 변수를 사용해주세요!

string s="";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알파벳 하나가 입력으로 주어지기 때문에 string 자료형이 아닌 char형으로 정의해줘도 돼요!

};

string wheelF(vector<wheel> &w, vector<string> &ans,int N, int K){//출력을 간단히 하기 위해 ans 벡터를 인자로 가져옴.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수 분리 좋아요~!👍

int temp=0;
for(int i=0; i<K; i++){
temp = (temp+w[i].n) % N;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

화살표의 위치를 잘 갱신해줬네요~👍

if(ans[temp] != ""&&ans[temp]!=w[i].s){
return "!";
}
auto it=find(ans.begin(), ans.end(), w[i].s);//중복 제거를 안해주면 틀린다.
Copy link

@kimhj010502 kimhj010502 Sep 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. find() 함수를 사용하여 알파벳 중복 체크를 해줬네요! 이 방법도 괜찮지만 find 연산의 시간복잡도는 O(n)으로 n이 커지게 되면 시간복잡도 측면에서 비효율적일 수 있어요! 알파벳 중복 확인 배열을 활용하는 방법도 있으니 참고해주세요~

if(!(it==ans.end())&&ans[temp]!=w[i].s){
return "!";
}
ans[temp] = w[i].s;
}
for(int i=0; i<N; i++){
if(ans[i] == ""){
ans[i] = "?";
}
}
Comment on lines +24 to +28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. main 함수에서 ans 벡터를 정의할 때 vector<char> ans(N, '?');로 해준다면 따로 이 과정을 거치지 않아도 되겠죠?

return "";
}

int main()
{
//변수 선언
int N, K, sum=0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. 알튜비튜에서 변수 이름은 스네이크 표기법으로 작성하도록 합니다! 클린코드 가이드를 확인해주세요~!


//입력
cin>>N>>K;

vector<wheel> w(K);//K만큼 할당 안해주고 값 입력받으면 for 문 안돌아간다.
vector<string> ans(N, "");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서도 마찬가지로 알파벳 하나가 입력으로 주어지기 때문에 string 자료형이 아닌 char형으로 정의해줘도 돼요!


for(int i=0; i<K; i++){
cin>>w[i].n>>w[i].s;
sum+=w[i].n;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

출력을 시작하는 인덱스를 따로 정의해줬군요! 아이디어 좋아요👍

}
string str;
str=wheelF(w,ans, N, K);

//출력
if(str=="!"){
cout<<str;
}
else{
int n=sum%N;//출력 시작 인덱스

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. n을 여러번 사용해줬네요 코드의 가독성과 쉬운 유지보수를 위해 최대한 다른 변수로 정의해주는 게 좋아요!

n+=N;
for(int i=n+N; i>n; i--){
cout<<ans[i%N];
}
}
return 0;
}
80 changes: 80 additions & 0 deletions 03_정수론/6588.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

const int RANGE=10000001;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

입력 범위를 상수로 선언해주셨군요! 아주 좋습니다👍👍


struct gold{
int a, b;//vector<int>를 여기다가 넣어서 한번에 처리하고 싶었는데 그러면 입력받는 코드 짜기가 어려워서 따로 뺐다.

string s;
};

vector<bool> isPrime(){
vector<bool> is_prime(RANGE, true);

is_prime[0]=is_prime[1]=false;

for(int i=2; i<sqrt(RANGE); i++){
if(is_prime[i]){//i가 소수라면
for(int j=i*i; j<=RANGE; j+=i){
is_prime[j]=false;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

에라토스테네스의 체의 구현도 완벽합니다😍😍

return is_prime;
}

vector<gold> goldbach(vector<int> n, vector<bool> &is_prime){//is_prime을 받을 때 &로 안받고 계속 메모리 새로 받는 것으로 했더니 시간 초과랑 runtime error : double free~ 떴었다.
//변수 선언
vector<gold> g(n.size());

int temp;
//소수 구하기 1000000이하의 짝수
for(int i=0; i<n.size(); i++){
for(int j=2; j<=n[i]; j++){//여기서 sqrt(n[i])하면 값이 2.8...가 나오기 때문에 sqrt를 하려면 반올림을 이용해 줘야 한다.
if (is_prime[j]){
temp=n[i]-j;
}
if(is_prime[temp]){
g[i].a=j;
g[i].b=temp;
break;
}
}
if(g[i].a==0) g[i].s="Goldbach's conjecture is wrong";
}
return g;

}


int main()
{
//변수 선언
vector<int> n;
vector<gold> g;
int temp;
int num;

//입력
while(cin>>temp&& temp!=0){
n.push_back(temp);
}

vector<bool> is_prime=isPrime();

g=goldbach(n, is_prime);

//출력
for(int i=0; i<g.size(); i++){
if(g[i].s!=""){
cout<<g[i].s<<"\n";
}
else
cout<<n[i]<<" = "<<g[i].a<<" + "<<g[i].b<<'\n';
}
return 0;
}