코딩테스트/프로그래머스 - LV 0
[프로그래머스] 문자열, 수학, 배열, 조건문
웅드
2024. 1. 12. 22:25
- 편지
#include <string>
#include <vector>
using namespace std;
int solution(string message) {
return message.length()*2;
}
- 가장 큰 수 찾기
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> array) {
vector<int> answer;
int max=0;
int max_index=0;
for(int i=0; i<array.size(); i++){
if(max < array[i]){
max = array[i];
max_index = i;
}
}
answer.push_back(max);
answer.push_back(max_index);
return answer;
}
- 문자열 계산하기
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int solution(string my_string) {
int answer = 0;
stringstream s(my_string);
s >> answer; // 첫 번째 숫자를 answer에 저장
int n;
char c;
while(s >> c >> n){ // 문자 숫자가 반복해서 나오기 때문에 s>>c>>n 으로 실행
if(c=='+') answer += n;
else answer -= n;
}
return answer;
}
- 배열의 유사도
#include <string>
#include <vector>
using namespace std;
int solution(vector<string> s1, vector<string> s2) {
int answer = 0;
for(int i=0; i< s1.size(); i++){
for(int j=0; j < s2.size(); j++){
if(s1[i] == s2[j]) answer++;
}
}
return answer;
}
반응형