2023.03.28
C++
https://www.acmicpc.net/problem/3052
3052๋ฒ: ๋๋จธ์ง
๊ฐ ์๋ฅผ 42๋ก ๋๋ ๋๋จธ์ง๋ 39, 40, 41, 0, 1, 2, 40, 41, 0, 1์ด๋ค. ์๋ก ๋ค๋ฅธ ๊ฐ์ 6๊ฐ๊ฐ ์๋ค.
www.acmicpc.net
๋ฌธ์
๋ ์์ฐ์ A์ B๊ฐ ์์ ๋, A%B๋ A๋ฅผ B๋ก ๋๋ ๋๋จธ์ง ์ด๋ค. ์๋ฅผ ๋ค์ด, 7, 14, 27, 38์ 3์ผ๋ก ๋๋ ๋๋จธ์ง๋ 1, 2, 0, 2์ด๋ค.
์ 10๊ฐ๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ค, ์ด๋ฅผ 42๋ก ๋๋ ๋๋จธ์ง๋ฅผ ๊ตฌํ๋ค. ๊ทธ ๋ค์ ์๋ก ๋ค๋ฅธ ๊ฐ์ด ๋ช ๊ฐ ์๋์ง ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
์ฒซ์งธ ์ค๋ถํฐ ์ด๋ฒ์งธ ์ค ๊น์ง ์ซ์๊ฐ ํ ์ค์ ํ๋์ฉ ์ฃผ์ด์ง๋ค. ์ด ์ซ์๋ 1,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๊ณ , ์์ด ์๋ ์ ์์ด๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์, 42๋ก ๋๋์์ ๋, ์๋ก ๋ค๋ฅธ ๋๋จธ์ง๊ฐ ๋ช ๊ฐ ์๋์ง ์ถ๋ ฅํ๋ค.
์์ ์ ๋ ฅ 1
1
2
3
4
5
6
7
8
9
10
์์ ์ถ๋ ฅ 1
10
๊ฐ ์๋ฅผ 42๋ก ๋๋ ๋๋จธ์ง๋ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10์ด๋ค.
์์ ์ ๋ ฅ 2
42
84
252
420
840
126
42
84
420
126
์์ ์ถ๋ ฅ 2
1
๋ชจ๋ ์๋ฅผ 42๋ก ๋๋ ๋๋จธ์ง๋ 0์ด๋ค.
์์ ์ ๋ ฅ 3
39
40
41
42
43
44
82
83
84
85
์์ ์ถ๋ ฅ 3
6
๊ฐ ์๋ฅผ 42๋ก ๋๋ ๋๋จธ์ง๋ 39, 40, 41, 0, 1, 2, 40, 41, 0, 1์ด๋ค. ์๋ก ๋ค๋ฅธ ๊ฐ์ 6๊ฐ๊ฐ ์๋ค.
์ฝ๋
#include <iostream>
using namespace std;
int main() {
int arr[10] = {};
int count = 0;
bool sameNum; // ๊ฐ์ ์๊ฐ ์กด์ฌํ๋์ง์ ๋ํ boolํ ๋ณ์
for (int i = 0; i < 10; i++) {
cin >> arr[i];
arr[i] = arr[i] % 42;
sameNum = false;
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
sameNum = true; // ๊ฐ์ ์๊ฐ ์กด์ฌ
break;
}
else {
continue;
}
}
if (sameNum == false) { //๋ชจ๋ arr[i]==arr[j]๊ฐ ์๋๊ฒฝ์ฐ
count++;
}
}
cout << count << endl;
return 0;
}
ํ์ด
์ฌ์ฉํ ๋ณ์:
- arr[10] : ์ ๋ ฅํ ๊ฐ์ ๋ฃ๊ธฐ ์ํ ๋ฐฐ์ด ๋ณ์
- count : ๋๋จธ์ง๊ฐ ๋ค๋ฅธ ์๊ฐ ๋ช๊ฐ ์๋์ง ์ธ๊ธฐ ์ํ intํ ๋ณ์
- sameName: ๊ฐ์ ์๊ฐ ์กด์ฌํ๋์ง ์๋์ง ๋น๊ตํ๊ธฐ ์ํ boolํ ๋ณ์
1. for ๋ฌธ์ ์ฌ์ฉํ์ฌ 10๊ฐ์ ์๋ฅผ ์ ๋ ฅํด arr ๋ฐฐ์ด์ ๋ฃ๋๋ค.
2. for ๋ฌธ์์ ์ด์ค for ๋ฌธ์ ์ฌ์ฉํ์ฌ ๊ฐ์ ์๊ฐ ์กด์ฌํ๋์ง ๋น๊ตํ๋ค.
1) arr[i]๋ฒ์งธ์ i๋ฒ์งธ ์ด์ ๋ฐฐ์ด๋ค์ ๋น๊ตํ๋ค.
2) sameNum์ด๋ผ๋ boolํ ๋ณ์๋ฅผ ์ด์ฉํ๋ค.
true๋ฉด ๊ฐ์ ์๊ฐ ์กด์ฌ, false๋ฉด ๊ฐ์ ์๊ฐ ์กด์ฌํ์ง ์์ count๋ฅผ 1์ฆ๊ฐ ํ ์ ์๊ฒ ํ๋ค.
3) for ๋ฌธ์ ๋น ์ ธ๋์ if๋ฌธ์ ํตํด sameNum๊ฐ false๋ฉด(๊ฐ์ ์๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด) count๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค.
4) ๋ฐ๋ณต๋ฌธ์ ์์ ํ ๋น ์ ธ๋์ค๋ฉด count๋ฅผ ์ถ๋ ฅํ๋ค.
๋ค๋ฅธ ๋ฐฉ๋ฒ
#include <iostream>
using namespace std;
int main() {
int arr[42] = {0,};
int n;
int count = 0;
for (int i = 0; i < 10; i++) {
cin >> n;
arr[n % 42] = 1;
}
for (int j = 0; j <= 42; j++) {
if (arr[j] == 1) count++;
}
cout << count << endl;
return 0;
}
clude <iostream>
using namespace std;
int main() {
int arr[42] = {0,};
int n;
int count = 0;
for (int i = 0; i < 10; i++) {
cin >> n;
arr[n % 42] = 1;
}
for (int j = 0; j <= 42; j++) {
if (arr[j] == 1) count += arr[j];
}
cout << count << endl;
return 0;
}
'Study > ์ฝํ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์ฝ๋ฌธ] chapter1. ์ฝ๋ฉ ํ ์คํธ (0) | 2024.09.21 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/C++] ๋ถ์์ ๋ง์ (0) | 2023.09.05 |
๋ฐฑ์ค ๋ธ๋ก ์ฆ2 8958๋ฒ: OXํด์ฆ (0) | 2023.03.20 |
๋ฐฑ์ค ๋ธ๋ก ์ฆ2 2577๋ฒ: ์ซ์์ ๊ฐ์ (0) | 2023.03.12 |
๋ฐฑ์ค ๋ธ๋ก ์ฆ3 2445๋ฒ: ๋ณ ์ฐ๊ธฐ - 8 (0) | 2023.02.25 |