23-06-26 계륵 일기

E woo·2023년 6월 26일

계륵 일기

목록 보기
3/31

여러 줄 입력

파이썬

while True:
    try:
        print(input())
    except EOFError:
        break

EOFError : End of File / 데이터가 없어 더 이상 값을 읽을 수 없을 때 에러

import sys

words = sys.stdin.readlines()
for word in words:
    print(word.rstrip())

C++

using namespace std;

int main()
{
    string str;

    while (1)
    {
        getline(cin, str);
        if (str == "")
            break;
        cout << str << "\n";
    }
    return 0;
}

백터 내적 계산

https://school.programmers.co.kr/learn/courses/30/lessons/12941
프로그래머스 문제 풀이 중 서로 크기가 같은 두 벡터의 시작부터 요소를 곱하는 과정이 필요했다.

내 코드

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

int solution(vector<int> A, vector<int> B)
{
    int answer = 0;
    
    sort(A.begin(), A.end());
    sort(B.begin(), B.end(), [](int a, int b){
        return a > b;
    });
    
    for(int i = 0; i < A.size(); i++)
    {
        answer += A[i] * B[i];
    }
    
    // [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
    cout << "Hello Cpp" << endl;

    return answer;
}

다른 사람의 풀이

#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int solution(vector<int> A, vector<int> B){
    sort(A.begin(),A.end());  sort(B.rbegin(),B.rend());
    return inner_product(A.begin(),A.end(),B.begin(),0);
}

https://en.cppreference.com/w/cpp/algorithm/inner_product

numeric 라이브러리의 inner_product 를 사용하면
두 백터 간의 내적 계산이 가능하다고 한다.

profile
뒘벼

0개의 댓글