티스토리 뷰

[1859] : 백만 장자 프로젝트

#include <iostream>
using namespace std;

int main(void) {
	int testCase;
	int day;
	cin >> testCase;
	long * ans = new long[testCase];
	for (int i = 0; i < testCase; i++) {
		cin >> day;
		long * price = new long[day];
		long total = 0;
		for (int j = 0; j < day; j++)
			cin >> price[j];
		long max = price[day - 1];
		for (int j = day - 2; j > -1; j--)
			if (price[j] < max) total += (max - price[j]);
			else max = price[j];
			ans[i] = total;
	}
	for (int i = 0; i < testCase; i++)
		cout << "#" << i + 1 << " " << ans[i] << endl;
	return 0;
}

[1948] : 날짜 계산기

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

int mth1, date1, mth2, date2;
vector<int> answer;

void printAnswer(int);
int cntDiff(int, int, int, int);

int main(void) {
	int testCase;
	cin >> testCase;
	for (int test = 0; test < testCase; test++) {
		cin >> mth1 >> date1 >> mth2 >> date2;
		answer.push_back(cntDiff(mth1, date1, mth2, date2));
	}
	printAnswer(testCase);
}

int cntDiff(int m1, int d1, int m2, int d2) {
	int diff;
	if (m1 == m2) diff = 0;
	else diff = d2; // m2월 1일 ~ m2월 d2일
	//m1 , m2사이에 있는 날짜 먼저 더하기
	for (int month = m1 + 1; month < m2; month++)
		switch (month) {
		case 1: case 3: case 5: case 7: case 8: case 10 : case 12 :
			diff += 31;
			break;
		case 2:
			diff += 28;
			break;
		default :
			diff += 30;
		}
	// m1월 마지막 일 - m1월 d1일 + 1
	switch (m1) {
	case 1: case 3: case 5: case 7: case 8: case 10: case 12:
		diff += (32 - d1);
		break;
	case 2:
		diff += (29 - d1);
		break;
	default:
		diff += (31 - d1);
	}

	return diff;
}

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

}

[1959] : 두 개의 숫자열

#include <iostream>
#include <vector>

using namespace std;

int N, M;
vector<int> answer;

int calMax(int*, int*, int);
void printAnswer(int);
int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        cin >> N >> M;
        int *Aj = new int[N];
        int *Bj = new int[M];
        for(int i = 0; i < N; i++) cin >> Aj[i];
        for(int i = 0; i < M; i++) cin >> Bj[i];
        if(N < M) answer.push_back(calMax(Aj, Bj, N));
        else answer.push_back(calMax(Bj, Aj, M));
    }
    printAnswer(testCase);
    return 0;
}

int calMax(int *Aj, int *Bj, int min){
    int max = 0, sum = 0, loop;
    loop = abs(N - M) + 1;
    for(int l = 0; l < loop; l++){
        sum = 0;
        for(int i = 0; i < min; i++)
            sum += (Aj[i] * Bj[l + i]);
        if(max < sum) max = sum;
    }
    return max;
}

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

[1966] : 숫자를 정렬하자

#include <iostream>
using namespace std;

int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        int length;
        int static trial = 1;
        cin >> length;
        int * arr = new int[length];
        for(int i = 0; i < length; i++)
            cin >> arr[i];
        for(int i = 0; i < length - 1; i++)
            for(int j = i + 1; j < length; j++)
                if(arr[j] < arr[i]){
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
        cout << "#" << trial << " ";
        for(int i = 0; i < length; i++)
            cout << arr[i] << " ";
        cout << endl;
        trial++;
    }
    return 0;
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함