def solution(dirs):
    answer = 0
    
    dx = [ 1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    strs = {"U": 0, "R": 1, "D": 2, "L": 3}
    
    visited = set()
    x, y = 0, 0
    
    for dir in dirs:
        
        i = strs[dir]
        nx, ny = x + dx[i], y + dy[i]
        if(nx < 6 and nx > -6 and ny < 6 and ny > -6):
            if (x, y, nx, ny) not in visited:
                visited.add((x, y, nx, ny))
                visited.add((nx, ny, x, y))
                answer +=1
            x, y = nx, ny            
    return answer

파이썬으로 푸는 것이 더 쉬워서 파이썬으로 풀이한 문제

 

#include <iostream>

using namespace std;

int solution(int n, int a, int b)
{
    int answer = 0;
    while(true){
        answer ++;
        if(a%2 ==0 )
            a = a/2;
        
        else
            a = a/2 + 1;
        if(b%2 == 0)
            b = b/2;
        else 
           b = b/2 + 1;
        if(a == b)
            break;
    }
    return answer;
}
#include <iostream>
using namespace std;

int solution(int n)
{
    int ans = 0;
	int tmp = 0;
    while(true){
        if(n == 1){
            ans++;
            break;
        }
        
        if(n%2 == 0){
            n = n/2;
        }
        else{
            ans++;
            n = n/2 ;
        }

    }
    return ans;
}
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

bool isit_prime(int n){
    for(int i=2; i <= sqrt(n) ; i++){
        if(n%i == 0)
        {
            return false;
        }
    }
    return true;
}
int solution(vector<int> nums) {
    int answer = 0;
    int tmp = 0;
    int first =0, second = 0, third  = 0;
    for(int i = 0; i < nums.size()-2; i++){
        first = nums[i];
        for(int j = i+1; j < nums.size()-1; j++){
            second = nums[j];
            for(int k = j+1; k < nums.size(); k++){
                third = nums[k];
                tmp = first + second + third;
                if(isit_prime(tmp)){
                    cout<< "tmp: " << tmp << endl;
                    answer+=1;
                }
            }
        }
    }
    return answer;
}

+ Recent posts