[Softeer] 난이도별 문제 풀이 1

CHAEN·2023년 1월 6일
0

problem solving

목록 보기
33/33
post-thumbnail

⭐️

- 주행거리 비교하기

C++

#include<iostream>

using namespace std;

int main(int argc, char** argv)
{
	int a, b;
	int result;

	scanf("%d %d", &a, &b);

	if (a > b) {
		printf("A");
	}
	else if (b > a) {
		printf("B");
	} else {
		printf("same");
	}

	return 0;
}

Python

import sys

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

if a > b:
    print("A")
elif b > a:
    print("B")
else:
    print("same")

- 근무시간

C++

#include<iostream>
#include<vector>
#include<string>
#include<sstream>

using namespace std;

vector<int> split(string str, char sep) {
    vector<int> answer;
    stringstream ss(str);
    string temp;
 
    while (getline(ss, temp, sep)) {
        answer.push_back(stoi(temp));
    }
 
    return answer;
}

int main(int argc, char** argv)
{
	string in_time;
	string out_time;
	vector<int> t1;
	vector<int> t2;
	int result = 0;

	for(int i = 0; i < 5; ++i) {
		cin >> in_time >> out_time;

		t1 = split(in_time, ':');
		t2 = split(out_time, ':');

		result += ((t2[0] - t1[0]) * 60 + (t2[1] - t1[1]));
		
		t1.clear();
		t2.clear();
	}
	
	printf("%d", result);

	return 0;
}

분명 더 쉬운 방법이 있을 것 같은데 아직 c++ 문자열 다루는 것이 너무 어렵다.

Python

import sys

result = 0 

for _ in range(5):
    in_time, out_time = input().split()

    h1, m1 = map(int, in_time.split(':'))
    h2, m2 = map(int, out_time.split(':'))

    result += ((h2 * 60 + m2) - (h1 * 60 + m1))

print(result)

- A+B

C++

#include<iostream>

using namespace std;

int main(int argc, char** argv)
{
	int t;
	scanf("%d", &t);

	for(int i = 0; i < t; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);

		printf("Case #%d: %d\n", i+1, a+b);
	}

	return 0;
}

Python

import sys

input = sys.stdin.readline

t = int(input())

for i in range(1, t+1):
    a, b = map(int, input().rstrip().split())
    print(f'Case #{i}: {a+b}')

⭐️⭐️

- 금고털이

C++

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

bool cmp(vector<int> &v1, vector<int> &v2){
	return v1[1] > v2[1];
}

int main(int argc, char** argv)
{
	int w, n, answer = 0;
	scanf("%d %d", &w, &n);
	
	vector<vector<int>> metal(n);

	for(int i = 0; i < n; ++i) {
		int m, p;
		
		scanf("%d %d", &m, &p);
		metal[i] = {m, p};
	}
	sort(metal.begin(), metal.end(), cmp);

	for(auto m:metal) {
		if(w > m[0]) {
			answer += (m[0] * m[1]);
			w -= m[0];
		} else {
			answer += (w * m[1]);
			break;
		}
	}

	printf("%d", answer);

	return 0;
}

Python

import sys
input = sys.stdin.readline

w, n = map(int, input().split())
tr = [tuple(map(int, input().split())) for _ in range(n)]
tr.sort(key = lambda x: -x[1])

answer = 0
for m, p in tr:
    if m <= w:
        answer += m*p
        w -= m
    else:
        answer += w*p
        break
print(answer)

- 8단변속기

C++

#include<iostream>

using namespace std;

int main(int argc, char** argv)
{
	int temp;
	scanf("%d", &temp);

	if(temp == 1) {
		for(int i = 2; i < 9; ++i) {
			int t;
			scanf("%d", &t);
			if(t != i) {
				printf("mixed");
				return 0;
			}
		}
		printf("ascending");
	}
	else if(temp == 8) {
		for(int i = 7; i > 0; --i) {
			int t;
			scanf("%d", &t);
			if(t != i) {
				printf("mixed");
				return 0;
			}
		}
		printf("descending");
	}
	else {
		printf("mixed");
	}
	
	return 0;
}

descending 오타나서 3번 틀렸다 😅

Python

import sys

input = sys.stdin.readline

transmission = list(map(int, input().split()))

if transmission == [1, 2, 3, 4, 5, 6, 7, 8]:
    print("ascending")
elif transmission == [8, 7, 6, 5, 4, 3, 2, 1]:
    print("descending")
else:
    print("mixed")

- 장애물 인식 프로그램

C++

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

vector<vector<int>> graph;
int n;

int dfs(int x, int y){
	int count = 0;

	if(x < 0 || y < 0 || x >= n || y >= n) {return 0;}
	if(graph[x][y]){
		graph[x][y] = 0;

		count = 1;
		count += dfs(x-1, y);
		count += dfs(x, y-1);
		count += dfs(x+1, y);
		count += dfs(x, y+1);
		return count;
	}
	return 0;
}

int main(int argc, char** argv)
{	
	scanf("%d", &n);
	graph = vector<vector<int>>(n, vector<int>(n, 0));

	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			scanf("%1d", &graph[i][j]);
		}
	}

	vector<int> answer;
	int temp;

	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			temp = dfs(i, j);
			if(temp){
				answer.push_back(temp);
			}
		}
	}
	printf("%d\n", answer.size());

	sort(answer.begin(), answer.end());
	for(auto a:answer) {
		printf("%d\n", a);
	}

	return 0;
}

Python

import sys

n = int(input())

graph = [list(map(int, input())) for _ in range(n)]

def dfs(x, y):
    if x < 0 or x >= n or y < 0 or y >= n:
        return 0
    if graph[x][y]:
        graph[x][y] = 0
        
        count = 1     
        count += dfs(x-1, y)
        count += dfs(x, y-1)
        count += dfs(x+1, y)
        count += dfs(x, y+1)
        return count
    return 0

answer = []
for i in range(n):
    for j in range(n):
        res = dfs(i, j)
        if res:
            answer.append(res)

print(len(answer))
answer.sort()
for a in answer:
    print(a)

- 지도 자동 구축

C++

#include<iostream>
#include<cmath>

using namespace std;

int main(int argc, char** argv)
{
	int n, answer;
	scanf("%d", &n);

	answer = int(pow(pow(2, n) + 1, 2));
	printf("%d", answer);

	return 0;
}

Python

import sys
input = sys.stdin.readline

n = int(input())
print((2**n + 1) ** 2)

- 비밀메뉴

C++

#include<iostream>
#include<string>

using namespace std;

int main(int argc, char** argv)
{
	int m, n, k;
	scanf("%d %d %d", &m, &n, &k);

	string secret, user;

	cin.ignore();
	getline(cin, secret);
	getline(cin, user);

	if (user.find(secret) != string::npos) {
    	printf("secret");
	}
	else {
		printf("normal");
	}

	return 0;
}

Python

import sys

input = sys.stdin.readline

m, n, k = map(int, input().split())
secret_order = input().strip().replace(" ", "")
user_order = input().strip().replace(" ", "")

answer = user_order.count(secret_order)
if answer:
    print("secret")
else:
    print("normal")

- 회의실예약

C++

Python

import sys

input = sys.stdin.readline

n, m = map(int, input().split())

rooms = {input().strip() : [False] * 9 for _ in range(n)}

for _ in range(m):
    room, start, end = input().strip().split()
    for i in range(int(start), int(end)):
        rooms[room][i-9] = True

rooms = sorted(rooms.items())

for i, v in enumerate(rooms):
    r= v[0] # 방 이름
    tt = v[1] # 회의 일정
    if i != 0:
        print("-" * 5)

    print("Room " + r + ":")

    if False not in tt:
        print("Not available")
    else:
        at = []
        for j in range(9):
            if not tt[j]:
                for k in range(j, 9):
                    if not tt[k]:
                        tt[k] = True
                        if k == 8:
                            at.append((j+9, k+10))
                    else:
                        at.append((j+9, k+9))
                        break
                        
        print(len(at), "available:")

        for s, e in at:
            print(str(s).zfill(2) + '-' + str(e))

마지막 1시간짜리 반례를 놓쳐서 한참 걸렸다.

- 전광판

C++

Python

- GBC

C++

#include<iostream>
#include<vector>

using namespace std;

int main(int argc, char** argv)
{
	vector<int> limits = {0};
	vector<int> check = {0};

	int n, m, answer= 0;
	cin >> n >> m;

	for(int i = 0; i < n; ++i) {
		int h, s;
		cin >> h >> s;
		vector<int> temp(h, s);
		limits.insert(limits.end(), temp.begin(), temp.end());
	}
	for(int i = 0; i < m; ++i) {
		int h, s;
		cin >> h >> s;
		vector<int> temp(h, s);
		check.insert(check.end(), temp.begin(), temp.end());
	}

	for(int i = 0; i < 101; ++i) {
		answer = check[i] - limits[i] > answer ? check[i] - limits[i] : answer;
	}

	printf("%d", answer);
	return 0;
}

Python

import sys

n, m = map(int, input().split())

limits = [0]
check = [0]

for _ in range(n):
    h, s = map(int, input().split())
    temp = [s for _ in range(h)]
    limits.extend(temp)

for _ in range(m):
    h, s = map(int, input().split())
    temp = [s for _ in range(h)]
    check.extend(temp)

answer = max([check[i] - limits[i] for i in range(101)])
print(answer)

- 바이러스

C++

#include<iostream>

#define MOD 1000000007

using namespace std;

int main(int argc, char** argv)
{
	int p, n;
	long long k;

	cin >> k >> p >> n;

	while(n--) {
		k = (k * p) % MOD ;
	}
	printf("%lld", k);

	return 0;
}

Python

import sys

input = sys.stdin.readline

k, p, n = map(int, input().split())
v = (k % 1000000007 * pow(p, n, 1000000007)) % 1000000007

print(v)


# v = k * (p ** n)    # 시간 많이 소요

# 분할 정복을 통한 거듭제곱 구현
# def power(a, n):
#     if n == 0:
#         return 1
    
#     x = power(a, n//2)
    
#     if n % 2:
#         return a * x * x
#     else:
#         return x * x
profile
공부중입니다

0개의 댓글

관련 채용 정보