코드 카타
3진법 뒤집기
자연수 n이 매개변수로 주어집니다. n을 3진법 상에서 앞뒤로 뒤집은 후, 이를 다시 10진법으로 표현한 수를 return 하도록 solution 함수를 완성해주세요.
제한사항
n은 1 이상 100,000,000 이하인 자연수입니다
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
long long thirdValue = 0;
long long answer = 0;
string result = "";
if (n == 0) { return 0; }
while (n > 0)
{
result += to_string(n % 3);
n /= 3;
}
thirdValue = stoll(result);
int muliplyNum = 1;
while (thirdValue > 0)
{
answer += (thirdValue % 10)* muliplyNum;
muliplyNum *= 3;
thirdValue /= 10;
}
return answer;
}
나는 풀 때 string 으로 전환하여 풀게 되었는데 다른 사람의 풀이를 보니 벡터를 활용해 자릿수 마다 숫자를 저장해 변환하였다.
벡터를 활용하면 자릿수를 계산하지 안해도 되니 편하다고 느껴 다음에 이와같은 문제는 벡터를 활용해 보려한다.
밑에는 다른 사람의 풀이를 가져왔다.
// 119wjw님의 풀이
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int answer = 0;
vector<int> v;
while(n > 0){
v.push_back(n%3);
n/=3;
}
int k = 1;
while(!v.empty()) {
answer += k*v.back();
v.pop_back();
k*=3;
}
return answer;
}
발제 6번 (진행 중)
- 회전 기능
- Tick(float DeltaTime)에서 AddActorLocalRotation() 사용하여 매 프레임 회전 처리
// header
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "RotationActor.generated.h"
UCLASS()
class MYPROJECT_API ARotationActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ARotationActor();
protected:
USceneComponent* SceneComp; // 루트 컴포넌트
UStaticMeshComponent* StaticMeshComp; // 스태틱 매시 컴포넌트
float RotationSpeed = 90.0f;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void Rotating(float DeltaTime);
};
// cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "RotationActor.h"
// Sets default values
ARotationActor::ARotationActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// 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("/Engine/EditorShellMaterials/M_ShellMaster.M_ShellMaster"));
if (MaterialAsset.Succeeded()) // 파일 찾기 성공시
{
StaticMeshComp->SetMaterial(0, MaterialAsset.Object);
}
}
// Called when the game starts or when spawned
void ARotationActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ARotationActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (!FMath::IsNearlyZero(RotationSpeed))
{
Rotating(DeltaTime);
}
}
void ARotationActor::Rotating(float DeltaTime)
{
AddActorLocalRotation(FRotator(0.0f, RotationSpeed * DeltaTime, 0.0f));
}