i) array 사용X
#include <iostream>
using namespace std;
int main()
{
int N;
int min = 1000001;
int max = -1000001;
int X = 0;
cin >> N;
if (N < 1 || N >1000000)
return 0;
for (int i = 0; i < N; i++) {
cin >> X;
if (X > max)
max = X;
if (X < min)
min = X;
}
cout << min << " " << max;
}
ii) array 사용 (new, delete 사용)
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
int* arr = new int[N];
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
sort(arr, arr + N);
cout << arr[0] << " " << arr[N - 1];
delete[] arr;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int X;
int max = 0;
int count = 0;
for (int i = 0; i < 9; i++) {
cin >> X;
if (X > max) {
max = X;
count = i+1;
}
}
cout << max << "\n";
cout << count;
return 0;
}
i) 나머지를 이용하여 구하기
#include <iostream>
using namespace std;
int main() {
int A, B, C;
int res = 0;
int arr[10] = {};
cin >> A >> B >> C;
res = A * B * C;
while (res != 0) {
arr[res % 10]++;
res /= 10;
}
for (int i = 0; i < 10; i++)
cout << arr[i] << "\n";
return 0;
}
ii) 문자열을 이용하여 구하기 '0'
#include <iostream>
#include <string>
using namespace std;
int main() {
int A, B, C;
int res = 0;
int arr[10] = {};
cin >> A >> B >> C;
res = A * B * C;
/************** 암기하기 *************/
string str = to_string(res);
for (char ch : str) {
arr[ch - '0']++;
}
/************************************/
for (int i = 0; i < 10; i++)
cout << arr[i] << "\n";
return 0;
}
#include <iostream>
using namespace std;
int main() {
int N[10] = {};
int arr[42] = {};
int count = 0;
for (int i = 0; i < 10; i++) {
cin >> N[i];
}
for (int i = 0; i < 10; i++) {
arr[N[i] % 42]++;
}
for (int i = 0; i < 42; i++) {
if (arr[i] >= 1)
count++;
}
cout << count;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N = 0;
cin >> N;
double *arr = new double[N];
double sum = 0;
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
sort(arr, arr + N);
for (int i = 0; i < N; i++) {
arr[i] = arr[i] / arr[N-1] * 100;
sum += arr[i];
}
cout << sum / N;
delete[] arr;
}
#include <iostream>
#include <string>
using namespace std;
int score(string &s) {
int hit = 0;
int sum = 0;
for (char &ch : s) {
if (ch == 'O') {
hit++;
sum += hit;
}
else {
hit = 0;
}
}
return sum;
}
int main() {
int N;
cin >> N;
int result;
string str;
for (int i = 0; i < N; i++) {
cin >> str;
result = score(str);
cout << result << "\n";
}
}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int C, N;
cin >> C;
for (int j = 0; j < C; j++) {
cin >> N;
if (N < 1 || N > 1000)
return 0;
int* arr = new int[N];
double sum = 0;
double cnt = 0;
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
for (int i = 0; i < N; i++) {
sum += arr[i];
}
sum = sum / N;
for (int i = 0; i < N; i++) {
if (arr[i] > sum)
cnt++;
}
cnt = (cnt / N) * 100;
cout << fixed;
cout.precision(3);
cout << cnt << "%" <<"\n";
}
}
소숫점 자리 고정
cout << fixed; >> 소수점을 고정시켜 표현
cout.precision(3); >> 소숫점 3자리까지 표현