문제 10818.
N개의 정수가 주어진다. 이때, 최솟값과 최댓값을 구하는 프로그램을 작성하기.
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int N, max, min;
do
{
cin >> N;
} while (N < 1 || N > 1000000);
int *array_rx = new int[N];
for (int i = 0; i < N; i++) cin >> array_rx[i];
max = array_rx[0];
min = array_rx[0];
for (int i = 0; i < N; i++)
{
if (array_rx[i] >= max) max = array_rx[i];
if (array_rx[i] <= min) min = array_rx[i];
}
cout << min << " " << max << endl;
delete[] array_rx;
return 0;
}
문제 2562.
9개의 서로 다른 자연수가 주어질 때,
이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하기.
#include <iostream>
using namespace std;
int main()
{
int arr[9];
int r;
for (int i = 0; i < 9; i++)
{
do
{
cin >> arr[i];
} while (arr[i] >= 100);
}
int max = arr[0];
for (int i = 0; i < 9; i++)
{
if (arr[i] >= max)
{
max = arr[i];
r = i + 1;
}
}
cout << max << endl << r << endl;
return 0;
}
문제 2577.
세 개의 자연수 A, B, C가 주어질 때 A × B × C를 계산한 결과에 0부터 9까지 각각의 숫자가 몇 번씩 쓰였는지를 구하는 프로그램을 작성하기.
#include <iostream>
using namespace std;
int main()
{
int A, B, C, sav;
cin >> A >> B >> C;
int res = A * B * C;
int res_txcount[10] = {0, };
for (int i = 0; i < 10; i++)
{
sav = res % 10;
res_txcount[sav] += 1;
res /= 10;
if (res == 0) break;
}
for (int i = 0; i < 10; i++)
{
cout << res_txcount[i] << endl;
}
return 0;
}
tip: 숫자 다 제거해서 0 나오면 break로 빠져나오자.
문제 3052.
두 자연수 A와 B가 있을 때, A%B는 A를 B로 나눈 나머지 이다. 예를 들어, 7, 14, 27, 38을 3으로 나눈 나머지는 1, 2, 0, 2이다.
수 10개를 입력받은 뒤, 이를 42로 나눈 나머지를 구한다. 그 다음 서로 다른 값이 몇 개 있는지 출력하는 프로그램을 작성하시오.
#include <iostream>
using namespace std;
int main()
{
int arr_rx[10] = { 0, };
int arr_cnt[42] = { 0, };
int chk(0);
for (int i = 0; i < 10; i++)
{
cin >> arr_rx[i];
arr_rx[i] %= 42;
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 42; j++)
{
if (arr_rx[i] == j)
{
arr_cnt[j] += 1;
}
}
}
for (int j = 0; j < 42; j++)
{
if (arr_cnt[j] != 0) chk++;
}
cout << chk << endl;
return 0;
}
문제 1546.
세준이는 기말고사를 망쳤다. 세준이는 점수를 조작해서 집에 가져가기로 했다. 일단 세준이는 자기 점수 중에 최댓값을 골랐다. 이 값을 M이라고 한다. 그리고 나서 모든 점수를 점수/M*100으로 고쳤다.
예를 들어, 세준이의 최고점이 70이고, 수학점수가 50이었으면 수학점수는 50/70*100이 되어 71.43점이 된다.
세준이의 성적을 위의 방법대로 새로 계산했을 때, 새로운 평균을 구하는 프로그램을 작성하시오.
동적 할당을 위한 new와 delete 매번 기입이 귀찮으니 벡터를 사용해보자. 알아서 메모리 할당 및 삭제를 해준다.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 0;
cin >> n;
int max = 0;
double whomped_sum = 0;
vector<int> vec_score;
// score 값 입력
for (int i = 0; i < n; i++)
{
int score = 0;
cin >> score;
vec_score.push_back(score);
}
// max값 판단
for (int i = 0; i < n; i++)
if (max <= vec_score.at(i)) max = vec_score.at(i);
// 조작된 평균
for (int i = 0; i < n; i++)
whomped_sum += (double)vec_score.at(i) / (double)max * 100;
double mean = whomped_sum / n;
cout << mean << endl;
return 0;
}
문제 8958.
"OOXXOXXOOO"와 같은 OX퀴즈의 결과가 있다. O는 문제를 맞은 것이고, X는 문제를 틀린 것이다. 문제를 맞은 경우 그 문제의 점수는 그 문제까지 연속된 O의 개수가 된다. 예를 들어, 10번 문제의 점수는 3이 된다.
"OOXXOXXOOO"의 점수는 1+2+0+0+1+0+0+1+2+3 = 10점이다.
OX퀴즈의 결과가 주어졌을 때, 점수를 구하는 프로그램을 작성하라.
string 클래스 문자열로 풀기.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int tcase;
string s;
cin >> tcase;
for (int j = 0; j < tcase; j++)
{
cin >> s;
int sum = 0, cnt = 0;
for (int i = 0; i < s.length(); i++) // s.size() 로 써도 문제없다.
{
// 문자 두개로만 연산이 이루어진다면 O 하나만 고려하고 식을 적을 수도 있다.
if (s[i] == 'O') cnt++;
else cnt = 0;
sum += cnt;
}
cout << sum << endl;
}
return 0;
}
문제 4344.
대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈 진실을 알려줘야 한다.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int T, n;
int score = 0;
double mean = 0;
double safe_ppl = 0;
cin >> T;
vector<int> v;
for (int i = 0; i < T; i++)
{
int cnt = 0;
cin >> n;
double sum = 0;
for (int j = 0; j < n; j++)
{
cin >> score;
v.push_back(score);
sum += (double)v.at(j);
}
mean = sum / (double)n;
for (int k = 0; k < n; k++)
{
if (v.at(k) > mean) cnt++;
}
// size 멤버 함수는 원소의 개수 리턴. 그냥 (double)n으로 둬도 ok.
safe_ppl = (double)cnt / (double)v.size();
printf("%.3f%%\n", safe_ppl * 100);
for (int j = 0; j < n; j++)
v.pop_back();
}
return 0;
}
'어플리케이션 > C++' 카테고리의 다른 글
백준 함수 문제 모음집 (0) | 2021.05.17 |
---|---|
백준 while 문제 모음집 (0) | 2021.05.08 |
백준 for 문제 모음집 (0) | 2021.05.03 |