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
'๐ง ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ธํ๋ฐ C++] 6. ์ซ์๋ง ์ถ์ถ (0) | 2024.03.15 |
---|---|
[์ธํ๋ฐ C++] 5. ๋์ด๊ณ์ฐ (0) | 2024.03.14 |
[์ธํ๋ฐ C++] 3. ์ง์ฝ์์ ํฉ (0) | 2024.03.14 |
[์ธํ๋ฐ C++] 2. ์์ฐ์์ ํฉ (0) | 2024.03.13 |
[์ธํ๋ฐ C++] 1. 1๋ถํฐ N๊น์ง M์ ๋ฐฐ์ํฉ (0) | 2024.03.13 |