백준 3058번 (짝수를 찾아라, C++) [BAEKJOON]

짝수를 찾아라

https://www.acmicpc.net/problem/3058

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초128 MB71314942455870.864%

문제

7개의 자연수가 주어질 때, 이들 중 짝수인 자연수들을 모두 골라 그 합을 구하고,

고른 짝수들 중 최솟값을 찾는 프로그램을 작성하시오.

예를 들어, 7개의 자연수 13, 78, 39, 42, 54, 93, 86가 주어지면 이들 중 짝수는 78, 42, 54, 86이므로

그 합은 78 + 42 + 54 + 86 = 260 이 되고, 42 < 54 < 78 < 86 이므로 짝수들 중 최솟값은 42가 된다.

입력

입력은 T개의 테스트 데이터로 구성된다.

입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다.

각 테스트 데이터는 한 줄로 구성되어 있고, 7개의 자연수가 공백으로 구분되어 있다.

입력으로 주어지는 자연수는 1보다 크거나 같고, 100보다 작거나 같다.

7개의 자연수 중 적어도 하나는 짝수이다.

출력

각 테스트 데이터에 대해,

7개 자연수 중 짝수의 합과 최솟값을 공백으로 구분하여 한 줄에 하나씩 출력한다.

예제 입력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
2
1 2 3 4 5 6 7
13 78 39 42 54 93 86
2 1 2 3 4 5 6 7 13 78 39 42 54 93 86
2
1 2 3 4 5 6 7
13 78 39 42 54 93 86

예제 출력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
12 2
260 42
12 2 260 42
12 2
260 42

알고리즘 분류


통과된 코드

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
int T, temp, res, minN;
int main()
{
ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
cin.tie(NULL);
cout.tie(NULL);
cin >> T;
while (T--) {
res = 0;
minN = INT32_MAX;
for (int i = 0; i < 7; i++) {
cin >> temp;
if (temp % 2 == 0) {
minN = min(temp, minN);
res += temp;
}
}
cout << res << " " << minN << "\n";
}
return 0;
}
#include <iostream> using namespace std; int T, temp, res, minN; int main() { ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화 // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다. cin.tie(NULL); cout.tie(NULL); cin >> T; while (T--) { res = 0; minN = INT32_MAX; for (int i = 0; i < 7; i++) { cin >> temp; if (temp % 2 == 0) { minN = min(temp, minN); res += temp; } } cout << res << " " << minN << "\n"; } return 0; }
#include <iostream>

using namespace std;

int T, temp, res, minN;

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> T;

	while (T--) {
		res = 0;
		minN = INT32_MAX;
		for (int i = 0; i < 7; i++) {
			cin >> temp;
			if (temp % 2 == 0) {
				minN = min(temp, minN);
				res += temp;
			}
		}

		cout << res << " " << minN << "\n";
	}

	return 0;
}

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다