코드카타
이상한 문자 만들기
문제 설명
문자열 s는 한 개 이상의 단어로 구성되어 있습니다. 각 단어는 하나 이상의 공백문자로 구분되어 있습니다. 각 단어의 짝수번째 알파벳은 대문자로, 홀수번째 알파벳은 소문자로 바꾼 문자열을 리턴하는 함수, solution을 완성하세요.
제한 사항
문자열 전체의 짝/홀수 인덱스가 아니라, 단어(공백을 기준)별로 짝/홀수 인덱스를 판단해야합니다.
첫 번째 글자는 0번째 인덱스로 보아 짝수번째 알파벳으로 처리해야 합니다.
#include <string>
#include <vector>
using namespace std;
string solution(string s) {
int blankCount = 0;
int stringCount = 0;
for (char c : s)
{
if (c == ' ')
{
blankCount = 0;
stringCount++;
continue;
}
if (blankCount % 2 == 0)
{
if (c >= 97 && s.at(c <= 122))
{
s.at(stringCount) = toupper(c);
}
}
else
{
if (c >= 65 && c <= 90)
{
s.at(stringCount) = tolower(c);
}
}
stringCount++;
blankCount++;
}
return s;
}
touopper, tolower 함수로 대소문자로 변경하는 함수를 알수 있었다.
삼총사
문제 설명
한국중학교에 다니는 학생들은 각자 정수 번호를 갖고 있습니다. 이 학교 학생 3명의 정수 번호를 더했을 때 0이 되면 3명의 학생은 삼총사라고 합니다. 예를 들어, 5명의 학생이 있고, 각각의 정수 번호가 순서대로 -2, 3, 0, 2, -5일 때, 첫 번째, 세 번째, 네 번째 학생의 정수 번호를 더하면 0이므로 세 학생은 삼총사입니다. 또한, 두 번째, 네 번째, 다섯 번째 학생의 정수 번호를 더해도 0이므로 세 학생도 삼총사입니다. 따라서 이 경우 한국중학교에서는 두 가지 방법으로 삼총사를 만들 수 있습니다.
한국중학교 학생들의 번호를 나타내는 정수 배열 number가 매개변수로 주어질 때, 학생들 중 삼총사를 만들 수 있는 방법의 수를 return 하도록 solution 함수를 완성하세요.
제한사항
3 ≤ number의 길이 ≤ 13
-1,000 ≤ number의 각 원소 ≤ 1,000
서로 다른 학생의 정수 번호가 같을 수 있습니다.
#include <vector>
using namespace std;
int solution(vector<int> number) {
int answer = 0;
int n = number.size();
for (int i = 0; i < n - 2; i++)
{
for (int j = i + 1; j < n - 1; j++)
{
for (int k = j + 1; k < n; k++)
{
if (number[i] + number[j] + number[k] == 0)
{
answer++;
}
}
}
}
return answer;
}
중복없는 경우의 수를 구현 하는 방법을 for문을 통해 구현해 볼 수 있어서 좋았다.
발제 6번 - LocationItem 제작
// cpp
#include "LocationItem.h"
ALocationItem::ALocationItem()
{
PrimaryActorTick.bCanEverTick = true;
StartLocation = FVector::ZeroVector;
MaxDistance = 50.0f;
MoveDirection = FVector::ForwardVector;
// SceneComponent를 생성하고 루트 설정
SceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
SetRootComponent(SceneComp);
// StaticMeshComponent를 생성, SceneComp에 Attach(하위종속)
StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComp"));
StaticMeshComp->SetupAttachment(SceneComp);
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("/Engine/BasicShapes/Cube.Cube"));
if (MeshAsset.Succeeded())
{
StaticMeshComp->SetStaticMesh(MeshAsset.Object);
}
static ConstructorHelpers::FObjectFinder<UMaterial> MaterialAsset(TEXT("/Game/Material/M_Basic.M_Basic"));
if (MaterialAsset.Succeeded())
{
StaticMeshComp->SetMaterial(0, MaterialAsset.Object);
}
}
void ALocationItem::BeginPlay()
{
Super::BeginPlay();
StartLocation = GetActorLocation(); // 액터 생성 지점 가져오기
// 방향은 제곱의 값이 1이여야 함.
MoveDirection.Normalize(); // 크기를 1로 만드는 함수. 즉, 방향 벡터만 남기는 함수.
FVector ForwardVector = GetActorForwardVector();
// 전방 벡터 삽입
MoveDirection.X *= ForwardVector.X;
MoveDirection.Y *= ForwardVector.Y;
MoveDirection.Z *= ForwardVector.Z;
}
void ALocationItem::MoveActor(float DeltaTime)
{
FVector CurrentLocation = GetActorLocation();
FVector MoveLocation = CurrentLocation + (MovingSpeed * MoveDirection * DeltaTime);
// 이동거리
FVector Distance = CurrentLocation - StartLocation;
SetActorLocation(MoveLocation);
// 시작위치 기록 StartLocation
// 현재위치 - 시작위치 = 이동거리 CurrentLocation - StartLocation = Distance
// 이동거리(벡터) -> 길이 Distance
// 길이 -> 최대 이동길이
// 거리 변수 한 개더 생성
if(Distance.Length() >= MaxDistance)
{
StartLocation = GetActorLocation(); // 액터 생성 지점 가져오기
MoveDirection *= -1;
if (GEngine)
{
FString DebugMessage = FString::Printf(TEXT("거리 | %s / 방향 : %s"), *Distance.ToString(), *MoveDirection.ToString());
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, DebugMessage);
}
}
}
void ALocationItem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
MoveActor(DeltaTime);
}
// header
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LocationItem.generated.h"
UCLASS()
class MYPROJECT_API ALocationItem : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ALocationItem();
protected:
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Item|Componenets");
USceneComponent* SceneComp;
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = "Item|Components");
UStaticMeshComponent* StaticMeshComp;
FVector StartLocation;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement|MaxDistance");
float MaxDistance;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement|MoveDirection");
FVector MoveDirection;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement|MovingSpeed");
float MovingSpeed = 20.0f;
virtual void BeginPlay() override;
void MoveActor(float DeltaTime);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
이 액터 클래스를 제작하면서 리플렉션 시스템과 벡터개념에 대해 간단히 이해 할 수 있었다.
