[BOJ] 1차원 배열

2020. 7. 21. 11:55BOJ

한번 걸어봅시다.

 

[BOJ 10818]

 

하나씩 받아서 바로바로 쓰면 되긴 하지만 1차원 배열이니 배열로 받아서 해봤습니다.

#include <stdio.h>
#include <limits.h>
#include <vector>

using namespace std;

pair<int, int> findminmax(const vector<int> & v){
    int min = INT_MAX;
    int max = INT_MIN;
    
    for(const int n : v){
        if(min > n)
            min = n;
        if(max < n)
            max = n;
    }
    
    return {min, max};
}

int main(){
    int N;
    scanf("%d",&N);
    vector<int> v(N);
    for(int k=0;k<N;++k){
        scanf("%d",&v[k]);
    }
    
    auto minmax = findminmax(v);
    printf("%d %d", minmax.first, minmax.second);
}

 

[BOJ 2562]

 

비슷하게 해주면 됩니다.

#include <stdio.h>
#include <limits.h>
#include <vector>

using namespace std;

int findmaxidx(const vector<int> & v){
    int idx = -1, max = INT_MIN;
    
    for(int k=0;k<v.size();++k){
        int n = v[k];
        if(max < n){
            idx = k;
            max = n;
        }
    }
    
    return idx;
}

int main(){
    vector<int> IN(9);
    for(int k=0;k<9;++k)
        scanf("%d", &IN[k]);
    int maxidx = findmaxidx(IN);
    printf("%d\n%d\n", IN[maxidx], maxidx+1);
}

 

[BOJ 2577]

 

설명대로 하면  됩니다.

#include <stdio.h>
#include <limits.h>
#include <vector>

using namespace std;

static_assert(999 * 999 * 999 < INT_MAX);

vector<int> countdigit(int A, int B, int C){
    int X = A * B * C;
    vector<int> digits(10, 0);
    do{
        digits[X % 10]++;
        X /= 10;
    } while(X != 0);

    return digits;
}

int main(){
    int A, B, C;
    scanf("%d%d%d",&A,&B,&C);
    
    auto digits = countdigit(A, B, C);
    for(int count : digits)
        printf("%d\n",count);
}

 

[BOJ 3052]

 

set을 써보았어요.

#include <stdio.h>
#include <vector>
#include <set>

using namespace std;

int count_unique_remainder(const vector<int> & v, int divisor){
    set<int> remainders;
    for(const int n : v)
        remainders.insert(n % divisor);
    return remainders.size();
}

int main(){
    vector<int> IN(10);
    for(int k=0;k<10;++k)
        scanf("%d", &IN[k]);
    printf("%d",count_unique_remainder(IN, 42));
}

 

[BOJ 1546]

 

그냥 원래 평균에 한번에 보정해주면 됩니다. 

#include <stdio.h>
#include <vector>

using namespace std;

double correct_mean(const vector<int> & scores){
    int sum = 0, max = 0;
    for(const int score : scores){
        sum += score;
        if(max < score)
            max = score;
    }
    
    return (double) sum / scores.size() * 100 / max;
}

int main(){
    int N;
    scanf("%d", &N);
    vector<int> scores(N);
    for(int k=0;k<N;++k)
        scanf("%d", &scores[k]);
    printf("%f", correct_mean(scores));
}

 

[BOJ 8958]

 

string으로 받아서 printf로 출력해보았어요. 저번 시간에 말한 것처럼 입출력 성능에는 안 좋아요.

#include <iostream>
#include <string>

using namespace std;

int calc_score(const string & scoreboard){
    int score = 0;
    int consecutive = 0;
    
    for(const char mark : scoreboard){
        if(mark == 'O'){
            score += (++consecutive);
        } else if(mark == 'X'){
            consecutive = 0;
        } else{
            //assert(0,"mark should be O or X");
        }
    }
    
    return score;
}

int main(){
    int T;
    scanf("%d",&T);
    string s;
    for(int k=0;k<T;++k){
        cin >> s;
        printf("%d\n",calc_score(s));
    }
}

 

[BOJ 4344]

 

마지막 문제라 그런가 소스가 좀 길어진 것 같네요. 평균이라는 것이 이렇게 위험할 수도 있는 거에요.

#include <stdio.h>
#include <vector>

using namespace std;

double ratio_super(const vector<int> & scores){
    int sum = 0;
    for(const int score : scores)
        sum += score;
    
    double mean = (double) sum / scores.size();
    
    int count = 0;
    for(const int score : scores)
        if(mean < score)
            count++;
    
    return (double) count / scores.size();
}

int main(){
    int C;
    scanf("%d",&C);
    vector<int> scores;
    for(int l=0;l<C;++l){
        int N;
        scanf("%d",&N);
        scores.resize(N);
        for(int k=0;k<N;++k)
            scanf("%d",&scores[k]);
        printf("%.3f%%\n",ratio_super(scores) * 100);
    }
}

 

다음 시간에 만나요~

'BOJ' 카테고리의 다른 글

[BOJ] 문자열  (0) 2020.07.23
[BOJ] 함수  (0) 2020.07.23
[BOJ] WHILE문  (0) 2020.07.21
[BOJ] FOR문  (0) 2020.07.20
[BOJ] 입출력과 사칙연산  (1) 2020.07.20