Study/Algorithm

(C) 2562번: 최댓값

hwooo 2022. 6. 6. 04:13

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

 

2562번: 최댓값

9개의 서로 다른 자연수가 주어질 때, 이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하시오. 예를 들어, 서로 다른 9개의 자연수 3, 29, 38, 12, 57, 74, 40, 85, 61 이 주어

www.acmicpc.net

 

 


코드

배열 X

#include <stdio.h>
int main() {
	int n, i, cnt, max = 0;

	for (i = 0; i < 9; i++) {
		scanf("%d", &n);
		if (max < n) max = n, cnt = i + 1;
	}

	printf("%d\n%d", max, cnt);
}

 

배열 O

#include <stdio.h>
int main() {
	int n, i, arr[9], max = 0, cnt;

	for (i = 0; i < 9; i++) scanf("%d", &arr[i]);

	for (i = 0; i < 9; i++) {
		if (max < arr[i])
        		max = arr[i], cnt = i;
	}

	printf("%d\n%d", max, cnt + 1);
}