- 홀수만 더하기
- 선언 , 초기화 위치 때문에 오류가 남 (선언, 초기화 위치 잘 보기)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main(void) {
freopen("input.txt", "r", stdin);
int T;
int N;
cin >> T;
for (int test_case = 1; test_case <= T; test_case++) {
int sum = 0;
for (int i = 0; i < 10; i++) {
cin >> N;
if (N % 2 == 1) {
sum += N;
}
}
cout << '#' << test_case << ' ' << sum <<'\n';
}
return 0;
}
- 평균값 구하기
- 소수 첫째자리에서 반올림 값은 round 함수 사용
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
using namespace std;
int main(void) {
int T;
cin >> T;
for (int test_case = 1; test_case <= T; test_case++) {
int sum = 0;
float avg = 0.0;
for (int i = 0; i < 10; i++) {
int N;
cin >> N;
sum += N;
avg = sum / 10.0;
}
cout << "#" << test_case << ' ' << round(avg)<< '\n';
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main(void) {
freopen("input.txt", "r", stdin);
int T;
cin >> T;
for (int test_case = 1; test_case <= T; test_case++) {
char sign;
int a, b;
cin >> a >> b;
if (a > b) {
sign = '>';
}
else if (a < b) {
sign = '<';
}
else if (a == b) {
sign = '=';
}
cout << "#" << test_case << ' ' << sign << '\n';
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main(void) {
int T;
cin >> T;
for (int test_case = 1; test_case <= T; test_case++) {
int max=0;
for (int i = 0; i < 10; i++) {
int N;
cin >> N;
if (max < N) {
max = N;
}
}
cout << "#" << test_case << ' ' << max << '\n';
}
return 0;
}
- 중간값 찾기
- 중간값은 통계 집단의 수치를 크기 순으로 배열 했을 때 전체의 중앙에 위치하는 수치를 뜻함, 크기 순으로 배열한다음에 중앙에 위치한 값을 찾는 문제
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> arr;
int N;
int main(void) {
freopen("input.txt", "r", stdin);
cin >> N;
arr.resize(N);
if ((9 <= N <= 199) && (N % 2 == 1)) {
for (int i = 0;i < arr.size(); i++) {
cin >> arr[i];
}
sort(arr.begin(), arr.end());
int mid;
mid = N / 2;
cout << arr[mid] << '\n';
}
}
- 자릿수 더하기
- 10으로 나눈 나머지로 일의 자리 숫자를 구한 다음,
- 10으로 나누는 것을 반복하여 십, 백, 천...의 자리를 각각 한자리씩 내리며 일의 자리 숫자를 구한다.
- 10으로 나눈 몫이 0이 되면 모든 자리 숫자를 얻은 것이므로 반복을 종료한다.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main(void) {
int sum = 0;
int N;
cin >> N;
for (int i = 0; i < 4; i++) {
int a;
a = N % 10;
N = N / 10;
sum += a;
}
cout << sum << '\n';
}
- 연월일 달력
- substring 메소드 등으로 자리수에 맞추어 월(month)과 일(day)을 구한다.
- 해당 일, 월이 포함되는지 확인한다.
#include <iostream>
#include <vector>
using namespace std;
vector<int> daysOfMonth = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int main() {
int T;
cin >> T;
string s;
for (int test_case = 1; test_case <= T; test_case++) {
cin >> s;
int month = atoi(s.substr(4, 2).c_str());
int day = atoi(s.substr(6).c_str());
cout << "#" << test_case << " ";
if (1 <= month && month <= 12 && 1 <= day && day <= daysOfMonth[month - 1]) {
cout << s.substr(0, 4) << "/" << s.substr(4, 2) << "/" << s.substr(6) << endl;
}
else {
cout << -1 << '\n';
}
}
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
cout << s.at(i) - 64<< ' ';
}
}