웅재의 코딩세상

[프로그래머스] 시뮬레이션, 조건문, 수학 본문

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

[프로그래머스] 시뮬레이션, 조건문, 수학

웅드 2024. 1. 19. 15:30
  • 문자열 밀기
#include <string>
#include <vector>

using namespace std;

int solution(string A, string B) {
    int answer = 0;
    if(A==B) return 0;
    while(1){
        answer++;
        char temp = A[A.size()-1];
        A.pop_back();
        A = temp + A;
        if(A==B){
            return answer;
        }
        if(answer > A.size()){
            return -1;
        }
    }
    return answer;
}
  • 종이 자르기
#include <string>
#include <vector>

using namespace std;

int solution(int M, int N) {
    int answer = 0;
    if(M==1 && N==1) return 0;
    answer += M-1;
    answer += M * (N-1);
    return answer;
}
  • 연속된 수의 합
#include <string>
#include <vector>

using namespace std;

vector<int> solution(int num, int total) {
    vector<int> answer;
    int a = total/num - num/2;
    int b = total/num + num/2;
    if(num %2 ==1){
        for(int i=a; i<=b; i++){
            answer.push_back(i);
        }
    }
    else{
        for(int i=a+1; i<=b;i++){
            answer.push_back(i);
        }
    }
    return answer;
}
  • 다음에 올 숫자
#include <string>
#include <vector>

using namespace std;

int solution(vector<int> common) {
    
    int an=0, bn=0;
    if(common[1] - common[0] == common[2] - common[1]) {
        an = common[1]-common[0];
        return common.back()+ an;
    }
    else{
        bn = common[1]/common[0]; 
        return common.back() * bn;
    }
}
반응형