#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

bool isit_prime(int n){
    for(int i=2; i <= sqrt(n) ; i++){
        if(n%i == 0)
        {
            return false;
        }
    }
    return true;
}
int solution(vector<int> nums) {
    int answer = 0;
    int tmp = 0;
    int first =0, second = 0, third  = 0;
    for(int i = 0; i < nums.size()-2; i++){
        first = nums[i];
        for(int j = i+1; j < nums.size()-1; j++){
            second = nums[j];
            for(int k = j+1; k < nums.size(); k++){
                third = nums[k];
                tmp = first + second + third;
                if(isit_prime(tmp)){
                    cout<< "tmp: " << tmp << endl;
                    answer+=1;
                }
            }
        }
    }
    return answer;
}
#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0;
    int tmp = 0;
    for(int i=1; i <= n/2; i++){
        tmp = i;
        for(int j =i+1; j <= n; j++){
            tmp += j;
            if(tmp == n){
                answer+=1;
                break;
            }
        }
    }
    answer+=1;
    return answer;
}

정확성 테스트에선 모두 맞았지만,, 효율성 테스트는 모두 시간 초과이다..이중 for문이어서 그런 것 같은데 더 줄일 방법을 생각해봐야겠다.

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

using namespace std;

vector<int> solution(int n, vector<string> words) {
    vector<int> answer;
    int flag = 0;
    int turn = 0;
    int turn_a = 0;
    int turn_b = 0;
    int num =0;
    //중복성 체크
    for(int i=0; i < words.size() -1 ; i++){
        for(int j = i+1; j < words.size(); j++){
            if(words[i] == words[j]){
                turn_a = j;
                break;
            }
        }
    }
    //겹치는지 확인
    for(int i=1; i< words.size();i++){
        string before = words[i-1];
        string now = words[i];
        int leng = before.length();
        if(before[leng-1] != now[0]){
            turn_b = i;
            break;
        }
    }
    if(turn_a!=0 && turn_b!=0){
        if(turn_a > turn_b)
            turn = turn_b;
        else
            turn = turn_a;
    }
    else{
        if(turn_a > turn_b)
            turn = turn_a;
        else
            turn = turn_b;
    }
    if(turn != 0){
        num = turn % n + 1;
        turn = turn / n + 1;
    }
    answer.push_back(num);
    answer.push_back(turn);
    return answer;
}

너무 케바케로 푼거같지만.....

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0;
    for(int i=1; i <= n; i++){
        if(n%i ==0)
            answer +=i;
    }
    return answer;
}

+ Recent posts