2025. 5. 27 오늘도 챗지피티랑 코딩공부 시작

챗지피티놈 무료버전이라 그런가 예시출력이 잘못나왔다.

마지막 항이 1x^0이 아니라 1-1이라 계수가 0이 되어서 3x^4 + 1x^3 + 3x^2 이렇게만 출력되어야한다.

// {계수, 차수} pair로 다항식 표현했음.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool comp(pair<int,int>a, pair<int,int>b){
    if(a.second == b.second) return a.first>b.first;
    else return a.second > b.second;
}
// 막무가내로 입력을 받더라도 차수 기준으로 정렬할 수 있도록 comp 만들어주기

int main(){
    int co, ex;
    vector<pair<int,int>>po1; // 다항식1
    vector<pair<int,int>>po2; // 다항식2
    vector<pair<int,int>>ans; // 더한거 넣을 곳
    
    while(1){
        cin >> co >> ex;
        if(co == -1 && ex == -1) break;
        po1.push_back({co,ex});
    } // 다항식 1 입력받기 반복
  
    while(1){
        cin >> co >> ex;
        if(co == -1 && ex == -1) break;
        po2.push_back({co,ex});
    } // 다항식 2 입력받기 반복
    
    sort(po1.begin(), po1.end(), comp); // 차수 순으로 정렬
    sort(po2.begin(), po2.end(), comp); 

    int st1 = 0, st2 =0;
    while(st1 <= po1.size() && st2 <= po2.size()){
        // 다항식 둘중 하나의 끝까지 갈때까지 반복하기
        
        // 차수가 다를 경우 차수 큰거 넣고, 그 다항식꺼 다음 항으로 넘어가기
        if(po1[st1].second > po2[st2].second){
            ans.push_back(po1[st1]);
            ++st1;
        } 
      
        else if(po1[st1].second < po2[st2].second){
            ans.push_back(po2[st2]);
            ++st2;
        }
        
        // 차수가 같을 경우 지수만 더하기
        else{
            int coef = po1[st1].first+po2[st2].first;
            if(coef != 0){
                ans.push_back({coef,po2[st2].second});
            }
            ++st1; ++st2;
        }
    }
    
    // 더하기 끝난 다항식 출력하기
    for(int i =0; i<ans.size(); ++i){
        cout << ans[i].first << "x^" << ans[i].second;
        if(i != (ans.size()-1)) cout << " + ";
    }
    return 0;
}

새로운 함수를 만드는 방법으로 변경 및 수정 제안 반영

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool comp(pair<int,int>a, pair<int,int>b){
    if(a.second == b.second) return a.first>b.first;
    else return a.second > b.second;
}

vector<pair<int,int>>ans;

void Sum(vector<pair<int,int>>po1, vector<pair<int,int>>po2){
    int st1 = 0, st2 =0;
    while(st1 < po1.size() && st2 < po2.size()){
        
        if(po1[st1].second > po2[st2].second){
            ans.push_back(po1[st1]);
            ++st1;
        }
        else if(po1[st1].second < po2[st2].second){
            ans.push_back(po2[st2]);
            ++st2;
        }
        else{
            int coef = po1[st1].first+po2[st2].first;
            if(coef != 0){
                ans.push_back({coef,po2[st2].second});
            }
            ++st1; ++st2;
        }
    }
    
    while (st1 < po1.size()) ans.push_back(po1[st1++]);
    while (st2 < po2.size()) ans.push_back(po2[st2++]);
}

int main(){
    int co, ex;
    vector<pair<int,int>>po1;
    vector<pair<int,int>>po2;
    
    while(1){
        cin >> co >> ex;
        if(co == -1 && ex == -1) break;
        po1.push_back({co,ex});
    }
    while(1){
        cin >> co >> ex;
        if(co == -1 && ex == -1) break;
        po2.push_back({co,ex});
    }
    
    sort(po1.begin(), po1.end(), comp);
    sort(po2.begin(), po2.end(), comp);
    
    Sum(po1, po2);
    
    for(int i =0; i<ans.size(); ++i){
        cout << ans[i].first << "x^" << ans[i].second;
        if(i != (ans.size()-1)) cout << " + ";
    }
    return 0;
}

리턴 값 가용 버전으로 바꿔서 해보자

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using Term = pair<int, int>; // (계수, 차수)

bool comp(Term a, Term b) {
    return a.second > b.second; // 차수 큰 순
}

vector<Term> Sum(const vector<Term>& po1, const vector<Term>& po2) {
    vector<Term> result;
    int st1 = 0, st2 = 0;
    
    while (st1 < po1.size() && st2 < po2.size()) {
        if (po1[st1].second > po2[st2].second) {
            result.push_back(po1[st1++]);
        } 
        
        else if (po1[st1].second < po2[st2].second) {
            result.push_back(po2[st2++]);
        } 
        
        else {
            int coef = po1[st1].first + po2[st2].first;
            if (coef != 0)
                result.push_back({coef, po1[st1].second});
            ++st1; ++st2;
        }
    }
    
    // 둘중 하나가 끝까지 갔다면 다른 하나가 남아있을 수도 있으니까
    // 반복문 돌려서 나머지 항들을 ans vector에 입력해야함
    while (st1 < po1.size()) result.push_back(po1[st1++]);
    while (st2 < po2.size()) result.push_back(po2[st2++]);

    return result;
}

int main() {
    int co, ex;
    vector<Term> po1, po2;

    while (cin >> co >> ex, !(co == -1 && ex == -1)) {
        po1.push_back({co, ex});
    }
    while (cin >> co >> ex, !(co == -1 && ex == -1)) {
        po2.push_back({co, ex});
    }

    sort(po1.begin(), po1.end(), comp);
    sort(po2.begin(), po2.end(), comp);

    vector<Term> ans = Sum(po1, po2);

    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i].first << "x^" << ans[i].second;
        if (i != ans.size() - 1) cout << " + ";
    }
    return 0;
}

'C++' 카테고리의 다른 글

배열  (0) 2025.06.17
제어문, 반복문  (0) 2025.06.17
변수+기본구성  (2) 2025.06.17
카데인 알고리즘  (0) 2025.06.15
C++ 간단 정리  (0) 2025.06.12

+ Recent posts