티스토리 뷰

[1970] : 쉬운 거스름돈

#include <iostream>

using namespace std;

void getChange(int);

int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        int changeM;
        cin >> changeM;
        getChange(changeM);
    }
    return 0;
}

void getChange(int money){
    int mType[8]{0,};
    int static trial = 1;
    while(money > 9){
        if(money >= 50000){
            mType[0] += 1;
            money -= 50000;
        }
        else if(money >= 10000){
            mType[1] += 1;
            money -= 10000;
        }
        else if(money >= 5000){
            mType[2] += 1;
            money -= 5000;
        }
        else if(money >= 1000){
            mType[3] += 1;
            money -= 1000;
        }
        else if(money >= 500){
            mType[4] += 1;
            money -= 500;
        }
        else if(money >= 100){
            mType[5] += 1;
            money -= 100;
        }
        else if(money >= 50){
            mType[6] += 1;
            money -= 50;
        }
        else{
            mType[7] += 1;
            money -= 10;
        }
    }
    cout << "#" << trial++ << endl;
    for(int i = 0; i < 8; i++)
        cout << mType[i] << " ";
    cout << endl;
}

[1979] : 어디에 단어가 들어갈 수 있을까

#include <iostream>
#include <vector>

using namespace std;

vector<int> answer;

int findBlank(int, int);
void printAnswer(int);

int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        int N, K;
        cin >> N >> K;
        answer.push_back(findBlank(N, K));
    }
    printAnswer(testCase);
    return 0;
}

int findBlank(int N, int K){
    int blank, answer = 0;
    int **board = new int*[N];
    for(int i = 0; i < N; i++)
        board[i] = new int[N];
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++)
            cin >> board[i][j];

    // 가로 탐색
    for(int i = 0; i < N; i++){
        blank = 0;
        for(int j = 0; j < N; j++){
            if(board[i][j] ==  1)
                blank++;
            else {
                if(blank == K)
                    answer++;
                blank = 0;
            }
        }
        if(blank == K)
            answer++;
    }
    // 세로 탐색
    for(int j = 0; j < N; j++){
        blank = 0;
        for(int i = 0; i < N; i++){
            if(board[i][j] == 1)
                blank++;
            else {
                if(blank == K)
                    answer++;
                blank = 0;
            }
        }
        if(blank == K)
            answer++;
    }
    return answer;

}

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

[1974] : 스도쿠 검증

#include <iostream>
#include <vector>

using namespace std;

vector<int> answer;
int sudoku[9][9];

int sudokuTrue();
void printAnswer(int);

int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                cin >> sudoku[i][j];
        answer.push_back(sudokuTrue());
    }
    printAnswer(testCase);
    return 0;
}

int sudokuTrue(void){
    int arr[10]{0,}; // 존재여부 검증에 쓰일 배열
    int index; // 칸 내에 있는 숫자 = arr에서 변경시킬 인덱스
    // 가로 검증
    for(int i = 0; i < 9; i++){
        for (int j = 0; j < 9; j++){
            index =  sudoku[i][j];
            arr[index]++;
            if(arr[index] > 1) return 0; // 같은 숫자 두번 이상 등장
        }
        for(int x = 1; x < 10; x++) arr[x] = 0; // 다시 arr의 값들 0으로 돌리기
    }
    // 세로 검증
    for(int j = 0; j < 9; j++){
        for(int i = 0; i < 9; i++){
            index = sudoku[i][j];
            arr[index]++;
            if(arr[index] > 1) return 0;
        }
        for(int x = 1; x < 10; x++) arr[x] = 0;
    }
    
    // 사각형 검증
    int cnt, i, j;
    // 1, 4, 7 box 검증
    for(cnt = 0; cnt < 3; cnt++){
        for(i = cnt * 3; i < (cnt + 1) * 3; i++){
            for(j = 0; j < 3; j++){
                index = sudoku[i][j];
                arr[index]++;
                if(arr[index] > 1) return 0;
            }
        }
        for(int x = 1; x < 10; x++) arr[x] = 0;
    }
    // 2, 5, 8 box 검증
    for(cnt = 0; cnt < 3; cnt++){
        // 1, 4, 7 box 검증
        for(i = cnt * 3; i < (cnt + 1) * 3; i++){
            for(j = 3; j < 6; j++){
                index = sudoku[i][j];
                arr[index]++;
                if(arr[index] > 1) return 0;
            }
        }
        for(int x = 1; x < 10; x++) arr[x] = 0;
    }
    
    for(cnt = 0; cnt < 3; cnt++){
        // 1, 4, 7 box 검증
        for(i = cnt * 3; i < (cnt + 1) * 3; i++){
            for(j = 6; j < 9; j++){
                index = sudoku[i][j];
                arr[index]++;
                if(arr[index] > 1) return 0;
            }
        }
        for(int x = 1; x < 10; x++) arr[x] = 0;
    }
    
    return 1;
}


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

[1983] : 조교의 성적 매기기

#include <iostream>
#include <vector>

using namespace std;

vector<int> answer;
int N, K;

void printAnswer(int);
int getRank(float*, float);

int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        int midS, finS, assS;
        cin >> N >> K;
        float *scores = new float[N];
        for(int i = 0; i < N; i++){
            cin >> midS >> finS >> assS;
            scores[i] = (midS * 0.35) + (finS * 0.45) + (assS * 0.2);
        }
        
        int rankK = getRank(scores, scores[K - 1]); // K의 등수
        int scoreSpace = N / 10; // 한 학점에 몇 명에 몇 명 분포
        int gpaK; // K의 학점 (A+ = 0)
        // 0 ≤ rankK < 3
        // 3 ≤ rankK < 6 .....
        for(int i = 0; i < 10; i++)
            if((rankK < (i + 1) * scoreSpace) && (i * scoreSpace <= rankK)){
                gpaK = i;
                break;
            }
        answer.push_back(gpaK);
        delete[] scores;
        scores = NULL;
    }
    printAnswer(testCase);
    return 0;
}

int getRank(float * scores, float scoreK){
    // sort
    float max;
    for(int i = 0; i < N - 1; i++){
        max = scores[i];
        for(int j = i + 1; j < N; j++){
            if (scores[j] > max){
                max = scores[j];
                float tmp = scores[j];
                scores[j] = scores[i];
                scores[i] = tmp;
            }
        }
    }
    for(int i = 0; i < N; i++)
        if(scores[i] == scoreK)
            return i; // i = K의 등수
    return -1;
}
void printAnswer(int testCase){
    for(int test = 0; test < testCase; test++){
        cout << "#" << test + 1 << " ";
        if(answer[test] == 0) cout << "A+" << endl;
        else if(answer[test] == 1) cout << "A0" << endl;
        else if(answer[test] == 2) cout << "A-" << endl;
        else if(answer[test] == 3) cout << "B+" << endl;
        else if(answer[test] == 4) cout << "B0" << endl;
        else if(answer[test] == 5) cout << "B-" << endl;
        else if(answer[test] == 6) cout << "C+" << endl;
        else if(answer[test] == 7) cout << "C0" << endl;
        else if(answer[test] == 8) cout << "C-" << endl;
        else cout << "D0" << endl;
    }
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함