반응형

 

 

 

 

 

www.acmicpc.net/problem/10757

 

10757번: 큰 수 A+B

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

www.acmicpc.net

 

 

코드

a, b = map(int, input().split())
print(a+b)

 

 

파이썬에서 다룰 수 있는 제일 큰 수는 몇일까?

파이썬은 10,000자리 자연수도 자유롭게 다룰 수 있다고 한다!!

 

 

 

 

 

파이썬 정수 최대값은 몇일까? 정최몇?

파이썬 2

import sys

t1 = sys.maxint
t2 = sys.maxint+1 #int범위를 넘으면 long으로 자동으로 형 변환

print(t1)
print(t2)
print(type(t1))
print(type(t2))
9223372036854775807
9223372036854775808
<type 'int'>
<type 'long'>

 

 

 

파이썬 3

import sys

t1 = sys.maxsize
t2 = sys.maxsize+1 #이것도 type int

print(t1)
print(t2)
print(type(t1))
print(type(t2))
9223372036854775807
9223372036854775808
<class 'int'>
<class 'int'>

 

 

파이썬3부터는 maxsize 넘어가도 int형으로 인식한다.

 

 

 

 

 

반응형
반응형

 

 

www.acmicpc.net/problem/3009

 

첫 번째 방법

a, b = input().split()
arrA_.append(a)
arrB_.append(b)
arrA_ = []
arrB_ = []

for i in range(3):
    a, b = input().split()
    arrA_.append(a)
    arrB_.append(b)

for i in range(3):
    if arrA_.count(arrA_[i]) == 1:
        ansA = arrA_[i]
    if arrB_.count(arrB_[i]) == 1:
        ansB = arrB_[i]

print(ansA, ansB)

 

 

두 번째 방법

a, b = map(int, input().split())
arrA_.append(a)
arrB_.append(b)
arrA_ = []
arrB_ = []

for i in range(3):
    a, b = map(int, input().split())
    arrA_.append(a)
    arrB_.append(b)

for i in range(3):
    if arrA_.count(arrA_[i]) == 1:
        ansA = arrA_[i]
    if arrB_.count(arrB_[i]) == 1:
        ansB = arrB_[i]

print(ansA, ansB)

 

 

 

결과

 

위에 두 번이 각각 첫 번째와 두 번째 방법을 사용해서 풀이한 것입니다. 아무런 차이가 없네요!

map은 뭘까요?

 

 

 

반응형
반응형

 

 

www.acmicpc.net/problem/11653

 

11653번: 소인수분해

첫째 줄에 정수 N (1 ≤ N ≤ 10,000,000)이 주어진다.

www.acmicpc.net

 

파이썬 코드

n = int(input())

idx = 2
while n != 1:
    if n % idx == 0:
        print(idx)
        n = n / idx
    else:
        idx += 1

 

C++ 코드

#include <stdio.h>

int main(void){
int n;
    scanf("%d", &n);

int idx = 2;
while(n != 1){
    if(n % idx == 0){
        printf("%d\n", idx);
        n /= idx;
    } else {
        idx += 1;
    }
}
}

 

 

결과

 

 

 

같은 로직으로 파이썬으로도 풀어보고, C++로도 풀어 보았는데 C++로 했을 때 시간이랑 메모리가 후어어ㅓ어어ㅓ얼씬 적게 드네요

왜 그럴까요?

 

 

 

 

반응형
반응형

 

 

 

1. 부분 자르기

string str;

str.substr(4);
str.substr(0, 10);

 

str.substr(n) : n번째 인덱스부터 끝까지 반환

str.substr(n, m) : n번째 인덱스부터 m개의 문자를 반환

 

 

 

2. printf로 출력하기

string str;

printf("%s\n", str.c_str());

printf로 출력하고 싶다면 .c_str()를 붙여주어야 한다.

 

 

 

3. 인덱스 사용하기

string str;

str[0];
str[4];

str[0] : 인덱스 0번째 문자를 반환한다.

str[4] : 인덱스 4번째 문자를 반환한다.

 

 

 

4. 공백을 포함하여 엔터가 나올때까지 입력받기

string str;

getline(cin, str);

getline(cin, str) 하면 엔터 나올때까지 공백을 포함하여 입력받을 수 있다.

 

 

5. 문자열 길이

str.length();

 

반응형
반응형

 

 

 

1. Bottom-up 방식

www.acmicpc.net/problem/9461

 

9461번: 파도반 수열

오른쪽 그림과 같이 삼각형이 나선 모양으로 놓여져 있다. 첫 삼각형은 정삼각형으로 변의 길이는 1이다. 그 다음에는 다음과 같은 과정으로 정삼각형을 계속 추가한다. 나선에서 가장 긴 변의

www.acmicpc.net

 

1, 1, 1, 2, 2, 3, 4, 5, 7, 9, ...

여기서 규칙이 보이나요?

규칙은 전전 값과 전전전 값을 더한 것입니다.

이를 bottom-up 방식으로 구현해보면 아래와 같습니다.

#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;

int total, N;
vector<long long int> v;

int main(void) {
    
    cin >> total;
    
    v.push_back(0);
    v.push_back(1);
    v.push_back(1);
    v.push_back(1);
    
    
    for(int k=0; k<total; k++){
        cin >> N;
        
        for(int i=(int)v.size(); i<=N; i++){
            v.push_back(v[i-2] + v[i-3]);
        }
        
        printf("%lld\n", v[N]);
    }
    
    return 0;
}

 

 

 

 

 

 

 

 

반응형

'Programming > Coding Test' 카테고리의 다른 글

백준 11653 - 파이썬 처음으로 사용해보기 !  (0) 2021.01.09
9. C++ String 문법 정리  (0) 2021.01.05
7. 순열과 조합 정리  (0) 2021.01.01
6. DFS - 부분집합  (0) 2020.12.31
5. STL 정리  (0) 2020.12.29
반응형

 

 

 

CASE 1) 순서 O, 중복 X

www.acmicpc.net/problem/15649

 

15649번: N과 M (1)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net

#include <stdio.h>
#include <iostream>
using namespace std;

int N, M;
int arr[9];
int ch[9];

void dfs(int depth){
    if(depth == M){
        for(int i=0; i<M; i++){
            printf("%d ", arr[i]);
        }
        printf("\n");
        return;
    } else {
        for(int i=1; i<=N; i++){
            if(ch[i] == 0){
                ch[i] = 1;
                arr[depth] = i;
                dfs(depth + 1);
                ch[i] = 0;
            }
        }
    }
}

int main(void) {
    
    cin >> N >> M;
    
    dfs(0);
    
    return 0;
}

 

- check 배열과 arr 배열, 그리고 depth 를 사용하여 구현하였다.

 

 

 

 

CASE 2) 순서 X,  중복 X

www.acmicpc.net/problem/15650

 

15650번: N과 M (2)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net

#include <stdio.h>
#include <iostream>
using namespace std;

int N, M;
int ch[9];
int arr[9];

void dfs(int s, int depth){
    if(depth == M){
        for(int i=0; i<M; i++){
            printf("%d ", arr[i]);
        }
        printf("\n");
        return;
    } else {
        for(int i=s; i<=N; i++){
            if(ch[i] == 0){
                ch[i] = 1;
                arr[depth] = i;
                dfs(i, depth+1);
                ch[i] = 0;
            }
        }
    }
}

int main(void) {
    
    cin >> N >> M;
    
    dfs(1, 0);
    
    return 0;
}

 

 

 

 

CASE 3) 순서 O, 중복 O

www.acmicpc.net/problem/15651 

 

15651번: N과 M (3)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net

#include <stdio.h>
#include <iostream>
using namespace std;

int N, M;
int arr[10];

void dfs(int depth){
    if(depth == M){
        for(int i=0; i<M; i++){
            printf("%d ", arr[i]);
        }
        printf("\n");
        return;
    } else {
        for(int i=1; i<=N; i++){
            arr[depth] = i;
            dfs(depth+1);
        }
    }
}

int main(void) {
    
    cin >> N >> M;
    
    dfs(0);
    
    return 0;
}

 

 

CASE 4) 순서 X, 중복 O

www.acmicpc.net/problem/15652

 

15652번: N과 M (4)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net

#include <stdio.h>
#include <iostream>
using namespace std;

int N, M;
int arr[10];


void dfs(int s, int depth){
    if(depth == M){
        for(int i=0; i<M; i++){
            printf("%d ", arr[i]);
        }
        printf("\n");
        return;
    } else {
        for(int i=s; i<=N; i++){
            arr[depth] = i;
            dfs(i, depth+1);
        }
    }
}

int main(void) {
    
    cin >> N >> M;
    
    dfs(1, 0);
    
    return 0;
}

 

 

 

< 결론 >

순서 X - 0부터 시작

순서 O - s 부터 시작

중복 X - ch 배열 안씀

중복 O - ch 배열 씀

 

 

 

 

반응형

'Programming > Coding Test' 카테고리의 다른 글

9. C++ String 문법 정리  (0) 2021.01.05
8. 동적계획법  (0) 2021.01.02
6. DFS - 부분집합  (0) 2020.12.31
5. STL 정리  (0) 2020.12.29
[C++] 백준 2573 빙산 - 테스트 케이스  (0) 2020.11.15

+ Recent posts