코드카타
크기가 작은 부분문자열
문제 설명
숫자로 이루어진 문자열 t와 p가 주어질 때, t에서 p와 길이가 같은 부분문자열 중에서, 이 부분문자열이 나타내는 수가 p가 나타내는 수보다 작거나 같은 것이 나오는 횟수를 return하는 함수 solution을 완성하세요.
예를 들어, t="3141592"이고 p="271" 인 경우, t의 길이가 3인 부분 문자열은 314, 141, 415, 159, 592입니다. 이 문자열이 나타내는 수 중 271보다 작거나 같은 수는 141, 159 2개 입니다.
제한사항
1 ≤ p의 길이 ≤ 18
p의 길이 ≤ t의 길이 ≤ 10,000
t와 p는 숫자로만 이루어진 문자열이며, 0으로 시작하지 않습니다.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(string t, string p) {
long long cutNum = 0;
vector<long long> str;
for (int i = 0; i < t.length() - (p.length() -1); i++)
{
cutNum = stoll(t.substr(i, p.length()));
str.push_back(cutNum);
}
int answer = 0;
long long compareNum = stoll(p);
for (const long long value : str)
{
if (value <= compareNum)
{
answer++;
}
}
return answer;
}
굳이 벡터에 자른 값을 저장하지 않고 바로바로 비교해서 카운트를 했어도 되는데 나눠서 아쉽다. 만약에 다음에 한다면 아래와 같이 for문을 하나로 합쳐서 간결하게 구현 할 것 같다.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(string t, string p) {
int answer = 0;
long long compareNum = stoll(p);
for (int i = 0; i <= t.length() - p.length(); i++)
{
if (stoll(t.substr(i, p.length())) <= compareNum)
{
answer++;
}
}
return answer;
}
문제 설명
명함 지갑을 만드는 회사에서 지갑의 크기를 정하려고 합니다. 다양한 모양과 크기의 명함들을 모두 수납할 수 있으면서, 작아서 들고 다니기 편한 지갑을 만들어야 합니다. 이러한 요건을 만족하는 지갑을 만들기 위해 디자인팀은 모든 명함의 가로 길이와 세로 길이를 조사했습니다.
아래 표는 4가지 명함의 가로 길이와 세로 길이를 나타냅니다.
명함 번호 가로 길이 세로 길이
1 60 50
2 30 70
3 60 30
4 80 40
가장 긴 가로 길이와 세로 길이가 각각 80, 70이기 때문에 80(가로) x 70(세로) 크기의 지갑을 만들면 모든 명함들을 수납할 수 있습니다. 하지만 2번 명함을 가로로 눕혀 수납한다면 80(가로) x 50(세로) 크기의 지갑으로 모든 명함들을 수납할 수 있습니다. 이때의 지갑 크기는 4000(=80 x 50)입니다.
모든 명함의 가로 길이와 세로 길이를 나타내는 2차원 배열 sizes가 매개변수로 주어집니다. 모든 명함을 수납할 수 있는 가장 작은 지갑을 만들 때, 지갑의 크기를 return 하도록 solution 함수를 완성해주세요.
제한사항
sizes의 길이는 1 이상 10,000 이하입니다.
sizes의 원소는 [w, h] 형식입니다.
w는 명함의 가로 길이를 나타냅니다.
h는 명함의 세로 길이를 나타냅니다.
w와 h는 1 이상 1,000 이하인 자연수입니다.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<vector<int>> sizes) {
int answer = 0;
int maxNum = 0;
int otherNum = 0;
for (vector<int> pair : sizes)
{
for (int inner : pair)
{
if (inner > maxNum)
{
maxNum = inner;
}
}
if (pair[0] > pair[1])
{
if (otherNum < pair[1])
{
otherNum = pair[1];
}
}
else
{
if (otherNum < pair[0])
{
otherNum = pair[0];
}
}
}
return maxNum * otherNum;
}
이차원 벡터내에서 가장 큰 수를 찾는다. 이제 안에서 둘중 작은수들 끼리의 가장큰수를 찾는다.
(이미 가장 큰수를 찾았기에 pair중 큰수는 max수보다 작아 무조건 들어간다. 그렇기에 작은수를 비교해 남은 가로or세로를 찾는다)
SpawnActor 구현 해보기
// Fill out your copyright notice in the Description page of Project Settings.
#include "Spawner.h"
#include "Kismet/KismetMathLibrary.h" // 랜덤 위치 계산을 위한 헤더
// Sets default values
ASpawner::ASpawner()
{
PrimaryActorTick.bCanEverTick = false;
SceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
SetRootComponent(SceneComp);
SpawnCount = 10;
SpawnRange = FVector(300.0f, 300.0f, 100.0f);
}
// Called when the game starts or when spawned
void ASpawner::BeginPlay()
{
Super::BeginPlay();
SpawnActors();
}
void ASpawner::SpawnActors()
{
if (!ActorSpawn) return; // 소환할 클래스가 설정되지 않았다면 리턴
UWorld* World = GetWorld();
if (World)
{
for (int i = 0; i < SpawnCount; ++i)
{
// 1. 현재 매니저 위치 기준 랜덤 위치 계산
FVector CenterLocation = GetActorLocation();
// UKismetMathLibrary를 사용하여 범위 내 랜덤 벡터 생성
FVector RandomLocation = UKismetMathLibrary::RandomPointInBoundingBox(CenterLocation, SpawnRange);
// 2. 회전값 설정 (필요시 랜덤으로 설정 가능)
FRotator RandomRotation;
RandomRotation.Yaw = UKismetMathLibrary::RandomFloatInRange(0.f, 360.f); // Z축 랜덤 값
RandomRotation.Pitch = UKismetMathLibrary::RandomFloatInRange(0.f, 360.f); // Y축 랜덤 값
RandomRotation.Roll = UKismetMathLibrary::RandomFloatInRange(0.f, 360.f); // X축 랜덤 값
// 3. 스폰 파라미터 설정 (충돌 시 처리 등)
FActorSpawnParameters SpawnParams;
//SpawnParams.Owner = this;
//SpawnParams.Instigator = GetInstigator();
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
// 4. 실제 소환 실행
World->SpawnActor<AActor>(ActorSpawn, RandomLocation, RandomRotation, SpawnParams);
}
}
}
