https://www.acmicpc.net/problem/10818
방법 1 - 460ms 275B
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
int li[N];
int max = -1000000;
int min = 1000000;
for (int i = 0; i < N; i++) {
cin >> li[i];
if (li[i] > max) max = li[i];
if (li[i] < min) min = li[i];
}
cout << min << " " << max;
}
방법 2 - 1번보다 더 오래 걸림 544ms 215B
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
int li[N];
for (int i = 0; i < N; i++) {
cin >> li[i];
}
sort(li, li + N);
cout << li[0] << " " << li[N-1];
}
3줄 추가 후 속도 향상 172ms 281B
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
int li[N];
for (int i = 0; i < N; i++) {
cin >> li[i];
}
sort(li, li + N);
cout << li[0] << " " << li[N-1];
}
https://www.acmicpc.net/problem/2562
방법 1 - 214B 0ms 2020KB
#include <iostream>
using namespace std;
int main() {
int li[9];
int m=0, idx=0;
for (int i = 0; i < 9; i++) {
cin >> li[i];
if (m < li[i]) {
m = li[i];
idx = i+1;
}
}
cout << m << "\n" << idx;
}
방법 2 - 166B 0ms 1112KB
#include <stdio.h>
int main(){
int s, max=0, idx;
for(int i=0; i<9; i++){
scanf("%d",&s);
if(max < s){
max=s;
idx=i+1;
}
}
printf("%d\n%d",max,idx);
}
https://www.acmicpc.net/problem/2577
배열 내부를 0으로 초기화
int n[10] = {0}
int n[10] = {}
방법 1 - 2020KB 0ms 231B
#include <iostream>
using namespace std;
int main(){
int A,B,C;
cin >> A >> B >> C;
int res = A*B*C;
int n[10]={0};
while (1){
if (res / 10 == 0){
n[res%10]++;
break;
}
else{
n[res%10]++;
res /= 10;
}
}
for (int i=0; i<10; i++){
cout << n[i] << "\n";
}
}
방법 2 - 2020KB 0ms 292B
#include <iostream>
using namespace std;
int main(){
int A,B,C;
cin >> A >> B >> C;
int res = A*B*C;
int n[10]={0};
while (res != 0){
n[res%10]++;
res /= 10;
}
for (int i=0; i<10; i++){
cout << n[i] << "\n";
}
}
방법 3 - 1112KB 0ms 197B
#include<stdio.h>
int main(){
int a,b,c,n[10]={};
scanf("%d %d %d",&a,&b,&c);
int res = a*b*c;
while(res != 0){
n[res%10]++;
res/=10;
}
for(int i=0; i<10; i++)
printf("%d\n",n[i]);
}
https://www.acmicpc.net/problem/3052
방법 1 - 2020KB 0ms 276B (for문 2개를 통한 distinct 값 도출)
#include <iostream>
using namespace std;
int main(){
int g, cnt=0, n[10]={};
for (int i=0; i<10; i++){
cin >> g;
n[i] = g % 42;
}
for (int i=0; i<10; i++){
int j;
for (j=0; j<i; j++){
if (n[i] == n[j])
break;
}
if (i==j)
cnt++;
}
cout << cnt;
}
방법 2 - 2020KB 0ms 209B (array 42개로 count)
#include <iostream>
using namespace std;
int main(){
int g, cnt=0, n[42]={};
for (int i=0; i<10; i++){
cin >> g;
n[g % 42]++;
}
for (int i=0; i<42; i++){
if (n[i] != 0) cnt ++;
}
cout << cnt;
}
방법 3 - 1112KB 0ms 199B
#include<stdio.h>
int main(){
int g, cnt=0, n[42]={};
for (int i=0; i<10; i++){
scanf("%d", &g);
n[g % 42]++;
}
for (int i=0; i<42; i++){
if (n[i] != 0) cnt ++;
}
printf("%d", cnt);
}
https://www.acmicpc.net/problem/1546
방법 1 - 2020KB, 0ms, 246B
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
double s=0, m=0, a[n]={};
for (int i=0; i<n; i++){
cin >> a[i];
if (m < a[i]) m = a[i];
}
for (int i=0; i<n; i++){
s = s + a[i] / m * 100;
}
cout << s/n;
}
방법 2 - 2020KB, 0ms, 215B
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
double s=0, m=0, a[n]={};
for (int i=0; i<n; i++){
cin >> a[i];
s += a[i];
if (m < a[i]) m = a[i];
}
s = s/m*100/n;
cout << s;
}
방법 3 - 1112KB, 0ms, 173B
#include<cstdio>
int main() {
int n,m=0,t;
double sum=0;
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%d",&t);
if(t>m) m=t;
sum+=t;
}
printf("%f",sum/m*100/n);
}
https://www.acmicpc.net/problem/8958
string.length 길이를 구할 수 있음
strlen(string) 길이를 구할 수 있음
strlen(char[~]) 길이를 구할 수 있음
방법 1 - 2024KB, 4ms, 339B
#include <iostream>
#include <string>
using namespace std;
int main(){
int n, inc=0, sum=0;
cin >> n;
string ox="";
for (int i=0; i<n; i++){
cin >> ox;
for (int j=0; j<ox.length(); j++){
if (ox[j]=='X') inc = 0;
else if (ox[j]=='O') inc++;
sum += inc;
}
cout << sum << "\n";
sum=0;
inc=0;
ox="";
}
}
방법 2 - 2020KB, 4ms, 349B
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char ox[80];
int n, inc=0, sum=0;
cin >> n;
for (int i=0; i<n; i++){
cin >> ox;
for (int j=0; j < strlen(ox); j++){
if (ox[j]=='X') inc = 0;
else if (ox[j]=='O'){
inc++;
sum += inc;
}
}
cout << sum << "\n";
sum=0;
inc=0;
}
}
https://www.acmicpc.net/problem/4344
소수점 자리 설정하기
cout << fixed;
cout.precision(3);
방법 1 - 2020KB, 4ms, 369B
#include <iostream>
using namespace std;
int main(){
int c,n;
cin >> c;
for (int i=0; i<c; i++){
cin >> n;
int g[n], cnt=0;
double avg=0;
for (int j=0; j<n; j++){
cin >> g[j];
avg += g[j];
}
avg = avg/n;
for (int k=0; k<n; k++){
if (g[k] > avg) cnt++;
}
cout << fixed;
cout.precision(3);
cout << cnt/(double)n * 100<< "%\n";
}
}
방법 2 - 1112KB, 0ms, 352B
#include <stdio.h>
using namespace std;
int main(){
int c,n;
scanf("%d", &c);
for (int i=0; i<c; i++){
scanf("%d", &n);
int g[n], cnt=0;
double avg=0;
for (int j=0; j<n; j++){
scanf("%d", &g[j]);
avg += g[j];
}
avg = avg/n;
for (int k=0; k<n; k++){
if (g[k] > avg) cnt++;
}
printf("%.3lf%%\n", (float)cnt/n*100);
}
}