count번 탈 때 필요한 총 금액을 구한다.price, 2 * price, 3 * price처럼 증가한다.money를 기준으로 부족한 금액을 반환하고, 부족하지 않다면 0을 반환한다.using namespace std;
long long solution(int price, int money, int count)
{
long long totalPrice = 1LL * price * count * (count + 1) / 2;
return max(0LL, totalPrice - money);
}
1LL을 곱해서 계산 전체를 long long으로 처리max(0LL, totalPrice - money)를 써서 반환부도 간단하게 정리| 정렬 | 전략 | 비교 횟수 | 교환 횟수 | 최선의 경우 |
|---|---|---|---|---|
| 버블 | 인접 비교 | O(n²) | O(n²) | O(n²) |
| 선택 | 최소값 찾기 | O(n²) | O(n) | O(n²) |
| 삽입 | 끼워넣기 | O(n²) | O(n²) | O(n) |
버블 정렬sort() 도 16개 이하의 데이터 정렬에는 삽입정렬을 쓴다.-나눠서 정렬하고 합치면 지수함수 원칙을 따르지 않으므로 훨씬 빨라진다
C++ sort 는 퀵 정렬을 기반으로 하되, 최악의 경우를 보정해 주기 위한 안전장치가 달린 Introsort 하이브리드 알고리즘
| 데이터 크기 | 사용 알고리즘 | 이유 |
|---|---|---|
| ~16개 이하 | 삽입 정렬 | 작은 데이터에서 빠름 |
| 일반적인 경우 | 퀵 정렬 | 평균 O(n log n), 캐시 친화 |
| 최악 감지 시 | 힙 정렬로 전환 | O(n²) 방지 |
PlayerController - 캐릭터에 빙의해서 조종할 수 있게 해주는 클래스GameModeBase가 일반적인 싱글플레이어에서 필요한 Base 클래스GameMode의 경우 GameModeBase의 자손. 멀티플레이어 기능 제공. PlayerState 등등 연동도 되어 있는 무거운 클래스.Edit - Project Settings - Maps&Modes - Default GameMode 에서 설정.Selected GameMode에서 디폴트 폰 클래스, 플레이어 컨트롤러 클래스, HUD 클래스 등등 다 설정 가능하다.Spectator Class 는 FPS 같은 데서 관전 같은 거 담당Window - World Settings 에서 게임모드 적용.Pawn으로 보통 구현Capsule Component(루트): 충돌판정관리Arrow Component: 방향 표시. 로직과 관계XMesh - 보통 Skeletal MeshCharacter Movement: 엔진 자체에서 제공하는 이동 로직 구현. Spring Arm + Camera 콤보public:
ASpartaCharacter();
USpringArmComponent* SpringArmComp;
UCameraComponent* CameraComp;
#include 할 수 있지만 낭비다. UCLASS 위에 class USpringArmComponent 이렇게 미리 선언만 해두는 게 나음.cpp 파일에 #include "Camera/CameraComponent.h", #include "GameFramework/SpringArmComponent.h" 하면 된다.SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
CameraComp->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);
CameraComp 의 경우 SpringArm에 붙이는데 이 때 "어디에 붙이냐" 를 결정해 주기 위해 USpringArmComponent::SocketName 하면 끝에 붙는다BP_MyGameMode 에서 Default Pawn Class에 내가 만든 캐릭터 클래스 넣어줘도 되고#include "SpartaCharacter.h"
ASpartaGameMode::ASpartaGameMode()
{
DefaultPawnClass = ASpartaCharacter::StaticClass();
}
StaticClass() 의 경우 UCLASS를 반환하는 함수. 객체를 실제로 생성하지 않고도 이렇게 대입할 수 있게 해 준다.