웅재의 코딩세상

[프로그래머스] dp, 수학, 조건문, 배열 본문

코딩테스트/프로그래머스 - LV 0

[프로그래머스] dp, 수학, 조건문, 배열

웅드 2024. 1. 19. 15:20
  • 저주의 숫자 3
#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = n;
    for(int i=1; i<n; i++){
        if(i % 3 == 0|| i % 10 == 3 || i /10 == 3){
            answer++;
        }
        while(1) {
            if(answer % 3 == 0 || answer / 10 == 3 || answer % 10 == 3 || (answer % 100) / 10 == 3)
                answer++;
            else break;
        }
    }
    return answer;
}
  • 평행
#include <string>
#include <vector>

using namespace std;

int eq(pair<vector<int>, vector<int>> a, pair<vector<int>,vector<int>> b){
    float x1 = a.first[0] - a.second[0];
    float x2 = b.first[0] - b.second[0];
    float y1 = a.first[1] - a.second[1];
    float y2 = b.first[1] - b.second[1];
    if(y1 / x1 == y2 / x2) return 1;
    return 0;
}

int solution(vector<vector<int>> dots) {
    int answer = 0;
    vector<int> a = dots[0];
    vector<int> b = dots[1];
    vector<int> c = dots[2];
    vector<int> d = dots[3];
    
    if(eq(make_pair(a,b),make_pair(c,d)) || eq(make_pair(a,c),make_pair(b,d)) || 
      eq(make_pair(a,d),make_pair(b,c))){
        return 1;
    }
    return 0;
}
  • 겹치는 선분의 길이
#include <string>
#include <vector>
#include <map>

using namespace std;

int solution(vector<vector<int>> lines) {
    int answer = 0;
    map<int, int> mp;
    for(auto i : lines){
        for(int j=i[0]; j< i[1]; ++j){
            mp[j]++;
        }
    }
    for(auto i : mp){
        if(i.second >= 2)answer ++;
    }
    return answer;
}
  • 유한소수 판별하기
#include <string>
#include <vector>

using namespace std;

int gcd(int a,int b){
    if(a%b == 0) return b;
    return gcd(b,a%b);
}

int solution(int a, int b) {
    b/= gcd(b,a);
    while(1){
        if(b % 2 == 0) b/=2;
        else if(b % 5 == 0) b/=5;
        else break;
    } //분모가 2 or 5로 이루어져 있으면 유한소수
    return b==1 ? 1 : 2;
}
반응형