조건 확인 좀 잘하자~!~~~~~~~~
1. 문자열 다루기 기본
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool solution(string s) {
bool answer = true;
if(s.size()!=4&&s.size()!=6){
answer=false;
}else{
for(int i=0;i<s.size();i++){
if(!(s[i]>='0'&&s[i]<='9')){
answer=false;
}
}
}
return answer;
}
2. 문자열을 정수로 바꾸기
#include <string>
#include <vector>
using namespace std;
int solution(string s) {
int answer = 0;
if(s.size()>=1 && s.size()<=5){
if(s[0]!='0')
answer=stoi(s);
}
return answer;
}
3. 문자열 내림차순으로 배치하기
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string s) {
string answer = "";
sort(s.begin(), s.end(), greater<char>());
answer=s;
return answer;
}
4. 수박수박수박수박수박수?
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(int n) {
string ans = "";
for(int i=0;i<n;i++){
if(i%2==0){
ans+="수";
}else{
ans+="박";
}
}
return ans;
}
5. 서울에서 김서방 찾기
#include <string>
#include <vector>
using namespace std;
string solution(vector<string> seoul) {
string answer = "";
answer+="김서방은 ";
for(int i=0;i<seoul.size();i++){
if(seoul[i]=="Kim"){
answer+=to_string(i); // int->string
}
}
answer+="에 있다";
return answer;
}
6. 문자열 내 p와 y의 개수
#include <string>
#include <iostream>
using namespace std;
bool solution(string s)
{
bool answer = true;
int cnt=0;
for(int i=0;i<s.size();i++){
if(s[i]=='p'||s[i]=='P'){
cnt++;
}else if(s[i]=='y'||s[i]=='Y'){
cnt--;
}
}
if(cnt!=0)
answer=false;
return answer;
}
7. 두 정수 사이의 합
#include <string>
#include <vector>
using namespace std;
long long solution(int a, int b) {
long long answer = 0;
//int sum=0;
if(a<b){
for(int i=a;i<=b;i++){
answer+=i;
}
}else{
for(int i=b;i<=a;i++){
answer+=i;
}
}
return answer;
}
다른 코드
#include <string>
#include <vector>
using namespace std;
long long solution(int a, int b) {
long long answer = 0;
int x = a<b?a:b;
int y = a<b?b:a;
for(int i=x;i<=y;i++){
answer+=i;
}
return answer;
}
8. 같은 숫자는 싫어
#include <vector>
#include <iostream>
using namespace std;
vector<int> solution(vector<int> arr)
{
vector<int> answer;
answer.push_back(arr[0]);
for(int i=1;i<arr.size();i++){
if(arr[i]!=arr[i-1])
answer.push_back(arr[i]);
}
return answer;
}
9. 나누어 떨어지는 숫자 배열
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> solution(vector<int> arr, int divisor) {
vector<int> answer;
for(int i=0;i<arr.size();i++){
if(arr[i]%divisor==0){
answer.push_back(arr[i]);
}
}
sort(answer.begin(), answer.end());
if(answer.empty())
answer.push_back(-1);
return answer;
}
10. 소수 찾기
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int answer = 0;
vector<bool> v(n+1, true);
for(int i = 2; i <= n; i++){
if(v[i] == true)
{
for(int j = 2; j*i <= n; j++)
{
v[j*i] = false;
}
answer++;
}
}
return answer;
}
'코딩 > Programmers 알고리즘' 카테고리의 다른 글
[C++] 위클리 챌린지 Level1 (0) | 2022.04.11 |
---|---|
[C++] Level 1 연습 문제 - 4 (0) | 2022.04.08 |
[C++] Level 1 연습 문제 - 2 (0) | 2022.04.08 |
[C++] Level 1 연습 문제 - 1 (0) | 2022.04.07 |
Level 1 직사각형 별찍기 (0) | 2018.09.07 |