웅재의 코딩세상
[프로그래머스] 시뮬레이션, 조건문, 수학 본문
- 문자열 밀기
#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;
}
}
반응형
'코딩테스트 > 프로그래머스 - LV 0' 카테고리의 다른 글
[프로그래머스] a와 b 출력하기 (0) | 2024.01.22 |
---|---|
[프로그래머스] 문자열 출력하기 (0) | 2024.01.22 |
[프로그래머스] 수학, 시뮬레이션, 문자열 (0) | 2024.01.19 |
[프로그래머스] 배열, 정렬, 문자열 (0) | 2024.01.19 |
[프로그래머스] dp, 수학, 조건문, 배열 (0) | 2024.01.19 |