๋ฌธ์ ์ดํดํ๊ธฐ
์ฌ์ค B๋ฅผ ์ ๋ ฌํ์ง๋ง๋ผ๊ณ ํ์ง๋ง ๊ฒฐ๊ณผ๊ฐ๋ง ํ๋จํ๊ธฐ๋๋ฌธ์ B๋ฅผ ์ ๋ ฌํด๋ ์๊ด์๊ธดํ๋ค..
ํ์ง๋ง B๋ ๊ทธ๋๋ก ๋๋๊ณ ํ์ด๋ณด๊ธฐ๋กํ๋ค!
์ต์๊ฐ์ ์ถ๋ ฅํด์ผํ๋ค. = ๊ฐ์ฅํฐ์์ ๊ฐ์ฅ์์์๋ฅผ ๊ณฑํ๊ฒํ๋ฉด๋๋ค.
A [1, 1, 1, 6, 0]
B [2, 7, 8, 3, 1]
tempB [2, 7, 8, 3, 1] : B๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํ ๋ฐฐ์ด
sortA [0, 1, 1, 1, 6] : A๋ฅผ ์ค๋ฆ์ฐจ์ํ ๋ฐฐ์ด
sortB [8, 7, 3, 2, 1] : B๋ฅผ ๋ด๋ฆผ์ฐจ์ํ ๋ฐฐ์ด
for (int i = 0; i < sortB.length; i++) {
for (int j = 0; j < tempB.length; j++) {
if(sortB[i] == tempB[j]){
A[j] = sortA[i];
}
}
}
์์์ฒ๋ผ B์ ํฐ์๋ถํฐ A์ ์์์๋ฅผ ๋์ ํ๋ฉด ํ๋ฆด ์ค ์์๋๋ฐ... ์ค๋ต์ฒ๋ฆฌ๋์๋ค.
์ฝ๋ ์์ฒด์๋ ๋ฌธ์ ๊ฐ์๋๋ฐ.. B ๋ฐฐ์ด์ ์ค๋ณต๊ฐ์ด ์๋ ํ ์คํธ์ผ์ด์ค๋ฅผ ํ๋ ๋ ์๊ฐํด๋ดค๋ค.
A [1, 1, 1, 6, 0, 2]
B [2, 7, 3, 7, 3, 1]
sortA [0, 1, 1, 1, 2, 6]
sortB [7, 7, 3, 3, 2, 1]
tempB [2, 7, 3, 7, 3, 1]
for (int i = 0; i < sortB.length; i++) {
for (int j = 0; j < tempB.length; j++) {
if(sortB[i] == tempB[j]){
System.out.print(j+", ");
A[j] = sortA[i];
}
}
}
//j = 1, 3, 1, 3, 2, 4, 2, 4, 0, 5,
// A [2, 1, 1, 1, 1, 6]
//sortA [0, 1, 1, 1, 2, 6]
//sortB [7, 7, 3, 3, 2, 1]
//tempB [2, 7, 3, 7, 3, 1]
sortB[0](=7) == tempB[1](=7) ์ผ๋ A[1] = sortA[0](=0)
sortB[0](=7) == tempB[3](=7) ์ผ๋ A[3] = sortA[0](=0)
sortB[1](=7) == tempB[1](=7) ์ผ๋ A[1] = sortA[1](=1)
sortB[1](=7) == tempB[3](=7) ์ผ๋ A[3] = sortA[1](=1)
์ค๋ณต์ผ๋ก ๊ฐ์ด ๋์ ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ฐ์๋์๋ค!!
sortB == tempB ์คํ์ 1๋ฒ๋ง ์ผ์ด๋๊ฒํ๊ณ , ํ๋ฒ ๋น๊ตํ ๊ฐ์ -1๋ก ๋ณ๊ฒฝํ์ฌ ์ค๋ณต์ ๋ง์๋ค
์ฝ๋
import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
class Treasure {
int N;
int[] A;
int[] B;
void scan_input(String inputed) {
InputStream in = new ByteArrayInputStream(inputed.getBytes());
System.setIn(in);
}
void scan() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
A = new int[N];
B = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) { A[i] = Integer.parseInt(st.nextToken()); }
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) { B[i] = Integer.parseInt(st.nextToken()); }
}
void process() {
Integer[] sortA = new Integer[N];
for (int i = 0; i < N; i++) { sortA[i] = A[i]; }
Integer[] sortB = new Integer[N];
for (int i = 0; i < N; i++) { sortB[i] = B[i]; }
Integer[] tempB = new Integer[N];
for (int i = 0; i < N; i++) { tempB[i] = B[i]; }
//tempA๋ ์ค๋ฆ์ฐจ์ tempB์ ๋ด๋ฆผ์ฐจ์
Arrays.sort(sortA);
Arrays.sort(sortB, Collections.reverseOrder());
for (int i = 0; i < sortB.length; i++) {
int overlap=0; //์ค๋ณต์ ๊ฑฐ๋ณ์
for (int j = 0; j < tempB.length; j++) {
if(sortB[i] == tempB[j] && overlap == 0){
overlap++; //์ค๋ณต๋ ๊ฐ์ 1๋ฒ๋ง ๋์ฌ ์ ์๋๋ก
A[j] = sortA[i];
tempB[j] = -1; //์์ ๊ฐ์ ๋ณ๊ฒฝํด์ ์ค๋ณต๊ฐ์ ์ ๊ฑฐ
// System.out.println(sortA[i]);
// System.out.println("tempB "+Arrays.toString(tempB));
}
}
}
// System.out.println("A "+Arrays.toString(A));
// System.out.println("B "+Arrays.toString(B));
int result = 0;
for (int i = 0; i < N; i++) {
result = result + (A[i] * B[i]);
}
System.out.println(result);
}
}
public class Main {
public static void main(String[] args) {
Treasure r = new Treasure();
try {
r.scan();
} catch (IOException e) {
e.printStackTrace();
}
r.process();
}
}
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
class MainTest {
@Test
void Test1(){
Treasure t = new Treasure();
t.scan_input("5\n" +
"1 1 1 6 0\n" +
"2 7 8 3 1");
try {
t.scan();
} catch (IOException e) {
e.printStackTrace();
}
t.process();
}
@Test
void Test2(){
Treasure t = new Treasure();
t.scan_input("3\n" +
"1 1 3\n" +
"10 30 20");
try {
t.scan();
} catch (IOException e) {
e.printStackTrace();
}
t.process();
}
@Test
void Test3(){
Treasure t = new Treasure();
t.scan_input("9\n" +
"5 15 100 31 39 0 0 3 26\n" +
"11 12 13 2 3 4 5 9 1");
try {
t.scan();
} catch (IOException e) {
e.printStackTrace();
}
t.process();
}
@Test
void Test4(){
Treasure t = new Treasure();
t.scan_input("6\n" +
"1 1 1 6 0 2\n" +
"2 7 3 7 3 1");
try {
t.scan();
} catch (IOException e) {
e.printStackTrace();
}
t.process();
}
}
์ฑ๊ณต!
'๐ฏ Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SWEA] 1206. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 1์ผ์ฐจ - View (0) | 2022.02.10 |
---|---|
SWEA 1204. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 1์ผ์ฐจ - ์ต๋น์ ๊ตฌํ๊ธฐ (0) | 2022.02.10 |
๋ฐฑ์ค 1946๋ฒ : ์ ์ ์ฌ์ (0) | 2021.10.26 |
๋ฐฑ์ค 2217๋ฒ : ๋กํ (์๊ฐ์ด๊ณผ ํด๊ฒฐ๊ณผ์ ) (0) | 2021.10.25 |
๋ฐฑ์ค 2839๋ฒ : ์คํ ๋ฐฐ๋ฌ (2) | 2021.10.23 |