๐Ÿง ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[์ธํ”„๋Ÿฐ C++] 4. ๋‚˜์ด์ฐจ์ด

peewoong 2024. 3. 14. 23:21

N๋ช…์˜ ๋‚˜์ด๊ฐ€ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด N๋ช…์˜ ์‚ฌ๋žŒ ์ค‘ ๊ฐ€์žฅ ๋‚˜์ด ์ฐจ์ด๊ฐ€ ๋งŽ์ด ๋‚˜๋Š” ๊ฒฝ์šฐ๋Š” ๋ช‡ ์‚ด์ผ๊นŒ์š”? ์ตœ๋Œ€ ๋‚˜์ด ์ฐจ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

10

13 15 34 23 45 65 33 11 26 42

 

์ถœ๋ ฅ

54

 

1ํŠธ (์‹คํŒจ)

๋”๋ณด๊ธฐ
#include <iostream>
using namespace std;

int main(){
	int count, dif, num;
	int min, max;
	List<int> ageList = new List<int>();
	
	cin >> count;
	for(int i = 0; i < count; ++i){
		cin >> num;
		ageList.Add(num);
	}
	
	min = ageList[0];
	max = ageList[1];
	for(int i = 0; i < ageList.Count; ++i){
		if(ageList[i] < min) {
			min = ageList[i];
		}
		else if(ageList[i] > max){
			max = ageList[i];
		}
		
		if(min < max){
			dif = max - min;
		}
	}
	
	cout << dif;
	return 0;
}

 

2ํŠธ (์„ฑ๊ณต)

#include <iostream>
using namespace std;

int main(){
	int count, num, dif;
	int max = -2147000000;
	int min = 2147000000;
	
	cin >> count;
	for(int i = 0; i < count; ++i){
		cin >> num;
		if(max < num) max = num;
		if(min > num) min = num;
	}
	
	dif = max - min;
	cout << dif;
	return 0;
}

freopen ํ™œ์šฉํ•˜๊ธฐ

#include <iostream>
using namespace std;

int main(){
	freopen("input.txt", "rt", stdin);
	int count, num, dif;
	int max = -2147000000;
	int min = 2147000000;
	
	cin >> count;
	for(int i = 0; i < count; ++i){
		cin >> num;
		if(max < num) max = num;
		if(min > num) min = num;
	}
	
	dif = max - min;
	cout << dif;
	return 0;
}

 

ํ•ด๋‹น ํด๋”์— ํ…์ŠคํŠธ ํ•˜๋‚˜ ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ

์ปดํŒŒ์ผ ํ•„์ˆ˜

์ฑ„์  ํŒŒ์ผ ๋Œ๋ฆด ๋•Œ๋Š” ์ฃผ์„์ฒ˜๋ฆฌํ•˜๊ธฐ

rt = read text