프로그래머스 홀짝에 따라 다른 값 변환하기 C++
#include #include using namespace std; int solution(int n) { int answer = 0; if(n%2==0){ for(int i=0; i
2023.09.23
프로그래머스 공배수 C++
#include #include using namespace std; int solution(int number, int n, int m) { int answer = 0; if(number%n==0 && number%m==0){ answer = 1; } else{ answer = 0; } return answer; }
2023.09.23
프로그래머스 n의 배수 C++
#include #include using namespace std; int solution(int num, int n) { int answer = 0; if(num%n == 0){ answer = 1; } else{ answer = 0; } return answer; } 짝수홀수 찾는 방법을 알고있다면 쉬울듯 하다.
2023.09.23
프로그래머스 두 수의 연산값 비교하기 C++
#include #include using namespace std; int solution(int a, int b) { int answer = 0; string a1 = to_string(a); string b1 = to_string(b); string a_b = a1 + b1; if(stoi(a_b)
2023.09.23
프로그래머스 더 크게 합치기 C++
#include #include using namespace std; int solution(int a, int b) { int answer = 0; string a1 = to_string(a); string b1 = to_string(b); int a1_b1 = stoi(a1+b1); int b1_a1 = stoi(b1+a1); if(a1_b1 < b1_a1) answer = b1_a1; else answer = a1_b1; return answer; } 흠..다른 방법으로 풀어볼려했지만 이 방법이 제일 나은듯하다 문자열끼리 더한후 int로 형 변환후 그 값을 비교해서 answer에 넣는것밖에는 모르겠다 ㅠㅜ
2023.09.22
프로그래머스 문자열 곱하기 C++
#include #include using namespace std; string solution(string my_string, int k) { string answer = ""; for(int i=0; i
2023.09.22
프로그래머스 문자 리스트를 문자열로 변환하기 C++
#include #include using namespace std; string solution(vector arr) { string answer = ""; for(int i=0; i
2023.09.22
프로그래머스 문자열 섞기 C++
#include #include #include using namespace std; string solution(string str1, string str2) { string answer = ""; for(int i=0; i
2023.09.22
반응형
#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0;
    if(n%2==0){
        for(int i=0; i<=n; i+=2){
            answer += i*i;
        }
    }
    else{
        for(int i=1; i<=n; i+=2){
            answer += i;
        }
    }
    
    return answer;
}
반응형
반응형
#include <string>
#include <vector>

using namespace std;

int solution(int number, int n, int m) {
    int answer = 0;
    
    if(number%n==0 && number%m==0){
        answer = 1;
    }
    else{
        answer = 0;
    }
    
    return answer;
}
반응형
반응형
#include <string>
#include <vector>

using namespace std;

int solution(int num, int n) {
    int answer = 0;
    if(num%n == 0){
        answer = 1;
    }
    else{
        answer = 0;
    }
    
    return answer;
}

짝수홀수 찾는 방법을 알고있다면 쉬울듯 하다.

반응형
반응형
#include <string>
#include <vector>

using namespace std;

int solution(int a, int b) {
    int answer = 0;
    string a1 = to_string(a);
    string b1 = to_string(b);
    string a_b = a1 + b1;
    
    if(stoi(a_b) <= 2*a*b ){
        answer = 2*a*b;
    }
    else{
        answer = stoi(a_b);
    }
    
    
    return answer;
}

처음 문제를 읽었을때 제대로 이해가 안됐었다..ㅋㅋㅋ 그래서 문제를 제대로 이해하고 푸는게 중요하다. 문제 출제자의 의도를 파악하고 문제를 푸는것!

반응형
반응형
#include <string>
#include <vector>

using namespace std;

int solution(int a, int b) {
    int answer = 0;
    
    string a1 = to_string(a);
    string b1 = to_string(b);
    
    int a1_b1 = stoi(a1+b1);
    int b1_a1 = stoi(b1+a1);
    
    if(a1_b1 < b1_a1) answer = b1_a1;
    else answer = a1_b1;
    
    
    
    return answer;
}

흠..다른 방법으로 풀어볼려했지만 이 방법이 제일 나은듯하다 문자열끼리 더한후 int로 형 변환후 그 값을 비교해서 answer에 넣는것밖에는 모르겠다 ㅠㅜ

반응형
반응형
#include <string>
#include <vector>

using namespace std;

string solution(string my_string, int k) {
    string answer = "";
    
    for(int i=0; i<k; i++){
        answer += my_string;
    }
    
    return answer;
}
반응형
반응형
#include <string>
#include <vector>

using namespace std;

string solution(vector<string> arr) {
    string answer = "";
    
    for(int i=0; i<arr.size(); i++){
        answer += arr[i];
    }
    
    return answer;
}
반응형
반응형
#include <string>
#include <vector>
#include <iostream>

using namespace std;

string solution(string str1, string str2) {
    string answer = "";
    
    for(int i=0; i<str1.length(); i++){
        answer += str1[i];
        answer += str2[i];
    }
    
    return answer;
}

이 코드와

#include <string>
#include <vector>
#include <iostream>

using namespace std;

string solution(string str1, string str2) {
    string answer = "";
    
    for(int i=0; i<str1.length(); i++){
        answer += str1[i] + str2[i];
    }
    
    return answer;
}

이 두개 코드의 차이점을 솔직히 잘 모르겠다 왜..안될까..

 

1번째는 각 스트링에서 배열값으로 문자를 1개씩 차례대로 answer에 넣는건 알겠다 1번이 답이니까

근데 2번째 코드도 str1과 str2의 각 원소를 더해서 answer에다가 2개씩 집어넣는것 아닌가?..

라고 생각했지만 문자열끼리 더해버리면 아스키코드의 값으로 더해버린다고한다. 그래서 코드에 오류가 나고 더해지지않은것. 중요한거 배웠다ㅎㅎ

 

결국 1번쨰는 문자를 1개씩 넣어서 문자열을 만들어가는거고

2번째는 문자끼리 더해버려서 아스키코드의 값으로 변환되어 다른 문자로 변해버린다.

반응형