티스토리 뷰

[1986] : 지그재그 숫자

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

using namespace std;
vector<int> answer;

void printAnswer(int);
int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        int num, sum = 0;
        cin >> num;
        for(int n = 1; n < num + 1; n++){
            if(n % 2 == 0) sum -= n;
            else sum += n;
        }
        answer.push_back(sum);
    }
    printAnswer(testCase);
    return 0;
}

void printAnswer(int testCase){
    for(int i = 0; i < testCase; i++)
        cout << "#" << i + 1 << " " << answer[i] << endl;
}

[1984]  : 중간, 평균값 구하기

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

using namespace std;
vector<int> answer;

void printAnswer(int);
int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        int num[10], max = 0, min = 10000, sum = 0;
        for(int i = 0; i < 10; i++) cin >> num[i];
        for(int i = 0; i < 10; i++){
            if(num[i] > max) max = num[i];
            if(num[i] < min) min = num[i];
            sum += num[i];
        }
        sum = sum - max - min;
        answer.push_back(round(sum / 8.0));
    }
    printAnswer(testCase);
    return 0;
}

void printAnswer(int testCase){
    for(int i = 0; i < testCase; i++)
        cout << "#" << i + 1 << " " << answer[i] << endl;
}

[1204] : 최빈 수  구하기

#include <iostream>
#include <vector>

using namespace std;

int score[1000]; // 점수 배열
vector<int> answer; // testCase 마다 정답
int mostFrequent(int *); // 최빈 정수를 구해줌
void printAnswer(int); // output

int main(void){
    int testCase, caseNum;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        cin >> caseNum;
        for(int i = 0; i < 1000; i++)
            cin >> score[i];
        answer.push_back(mostFrequent(score));
    }
    printAnswer(testCase);
    return 0;
}

int mostFrequent(int * score){
    int freq[101] = {0, }, s_Score;
    // freq[101] : 해당 index에 횟수 count 할 것 (freq[0]은 해당사항 없음)
    // s_Score : score[i]에 있는 점수
    for(int i = 0; i < 1000; i++){
        s_Score = score[i];
        freq[s_Score]++;
    }
    
    int maxCount = 0;
    vector<int> maxScores;
    // 몇 번 호출 된 것이 가장 클까?
    for(int i = 1; i < 101; i++)
        if (freq[i] > maxCount)
            maxCount = freq[i];
    for(int i = 1; i < 101; i++)
        if(freq[i] == maxCount)
            maxScores.push_back(i);
    return maxScores.back();
}

void printAnswer(int testCase){
    for(int i = 0; i < testCase; i++)
        cout << "#" << i + 1 << " " << answer[i] << endl;
}

[1284]

#include <iostream>
#include <vector>

using namespace std;

int score[1000]; // 점수 배열
vector<int> answer; // testCase 마다 정답
int mostFrequent(int *); // 최빈 정수를 구해줌
void printAnswer(int); // output

int main(void){
    int testCase, caseNum;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        cin >> caseNum;
        for(int i = 0; i < 1000; i++)
            cin >> score[i];
        answer.push_back(mostFrequent(score));
    }
    printAnswer(testCase);
    return 0;
}

int mostFrequent(int * score){
    int freq[101] = {0, }, s_Score;
    // freq[101] : 해당 index에 횟수 count 할 것 (freq[0]은 해당사항 없음)
    // s_Score : score[i]에 있는 점수
    for(int i = 0; i < 1000; i++){
        s_Score = score[i];
        freq[s_Score]++;
    }
    
    int maxCount = 0;
    vector<int> maxScores;
    // 몇 번 호출 된 것이 가장 클까?
    for(int i = 1; i < 101; i++)
        if (freq[i] > maxCount)
            maxCount = freq[i];
    for(int i = 1; i < 101; i++)
        if(freq[i] == maxCount)
            maxScores.push_back(i);
    return maxScores.back();
}

void printAnswer(int testCase){
    for(int i = 0; i < testCase; i++)
        cout << "#" << i + 1 << " " << answer[i] << endl;
}

'CS > 알고리즘' 카테고리의 다른 글

[SWEA] D2 : 1859, 1948, 1959, 1966  (0) 2019.06.16
[SWEA] D2 : 1285, 1288, 1976, 1945  (0) 2019.06.15
[SWEA] D2 : 1926, 1989, 2005, 2007  (0) 2019.06.15
[SWEA] D1 : 1545, 1933, 1936, 2019  (0) 2019.06.15
[SWEA] D1 : 2046, 2047, 2050, 2056, 2058  (0) 2019.06.15
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함