Last active
April 23, 2018 10:18
-
-
Save qkreltms/bdc75e62466e62013ac3f1ae5d45fb99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package algorithm; | |
import java.util.*; | |
public class Kakao1 { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int n = sc.nextInt(); | |
sc.nextLine(); //flush | |
if (!(0 <= n && n <= 16)) return; | |
Kakao1 k = new Kakao1(); | |
int[] mapA = new int[n*n]; | |
int[] mapB = new int[n*n]; | |
int i = 0; | |
StringTokenizer st = new StringTokenizer(sc.nextLine()); | |
while (st.hasMoreTokens()) { | |
mapA[i++] = Integer.parseInt(st.nextToken()); | |
} | |
i = 0; | |
st = new StringTokenizer(sc.nextLine()); | |
while (st.hasMoreTokens()) { | |
mapB[i++] = Integer.parseInt(st.nextToken()); | |
} | |
String[] map = k.combineTwoMaps(mapA, mapB, n); | |
k.printMap(map, n); | |
} | |
public String[] combineTwoMaps(int[] mapA, int[] mapB, int n) { | |
int a = 1; //맨 앞의 비트가 0일수 있으므로 1로해서 0을 보존함. | |
int b = 1; | |
int ctn = 0; | |
String[] bitSum = new String[n]; | |
for (int i = n; i <= n*n; i+=n) { | |
for (int j = i-n; j <= i-1; j++) { | |
a = a<<1; | |
b = b<<1; | |
if (mapA[j] == 1) a|=1; | |
if (mapB[j] == 1) b|=1; | |
} | |
bitSum[ctn++] = Integer.toBinaryString(a|b); | |
a = 1; //맨 앞의 비트가 0일수 있으므로 1로해서 0을 보존함. | |
b = 1; | |
} | |
return bitSum; | |
} | |
public void printMap(String[] map, int n) { | |
for (int i = 0; i < n; i++) { | |
for (int j = 1; j< map[i].length(); j++) { | |
if (map[i].charAt(j) == '1') { | |
System.out.print("#"); | |
} else { | |
System.out.print("0"); | |
} | |
} | |
System.out.println(""); | |
} | |
} | |
} | |
#----------------------------------------------------------------------------------------------# | |
#python | |
def kakaotalk1_1(n, arr1, arr2): | |
ans = [] | |
for i in range(n): | |
ans.append(bin(arr1[i] | arr2[i]).lstrip('0b').replace("1", "#").replace("0", " ")) | |
print(ans) | |
kakaotalk1_1(6, [46, 33, 33 ,22, 31, 50], [27 ,56, 19, 14, 14, 10]) | |
kakaotalk1_1(5, [9, 20, 28 ,18, 11], [30 ,1, 21, 17, 28]) | |
#----------------------------------------------------------------------------------------------# | |
//kotlin 버전 | |
fun kakaotalk1_1(n: Int, arr1: Array<Int>, arr2: Array<Int>): MutableList<String> { | |
var ans: MutableList<String> = mutableListOf() | |
for (i in 0 until n) { | |
ans.add((arr1[i] or arr2[i]).toString(2).replace("1", "#").replace("0", " ")) | |
} | |
return ans | |
} | |
fun main(args: Array<String>) { | |
print(kakaotalk1_1(6, arrayOf(46, 33, 33, 22, 31, 50), arrayOf(27, 56, 19, 14, 14, 10))) | |
print(kakaotalk1_1(5, arrayOf(9, 20, 28 ,18, 11), arrayOf(30 ,1, 21, 17, 28))) | |
} | |
#----------------------------------------------------------------------------------------------# | |
//java | |
import java.util.ArrayList; | |
public class kakaotalk1_1 { | |
public static void main(String[] args) { | |
System.out.print(kakaotalk(6, new int[]{46, 33, 33, 22, 31, 50}, new int[]{27, 56, 19, 14, 14, 10})); | |
} | |
public static ArrayList<String> kakaotalk(int n, int[] arr1, int[] arr2) { | |
ArrayList<String> ans = new ArrayList<>(); | |
for (int i = 0; i < n; i++) { | |
ans.add(Integer.toString(arr1[i] | arr2[i], 2).replace("1", "#").replace("0", " ")); | |
} | |
return ans; | |
} | |
} | |
//function secretMap(n, arr1, arr2) { | |
// const result = []; | |
// for(let i = 0; i < n; i++){ | |
// result[i] = (arr1[i] | arr2[i]).toString(2).replace(/1/g, '#').replace(/0/g, ' '); | |
// } | |
// return result; | |
//} | |
// | |
//console.log(secretMap(6, [46, 33, 33 ,22, 31, 50], [27 ,56, 19, 14, 14, 10])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ans.add(Integer.toString(arr1[i] | arr2[i], 2).replace("1", "#").replace("0", " ")); 같은 경우는 다른 답을 출력할 것 같습니다.
1의 경우 의도한 값은 " 1"이겠지만 실제로는 "1"만 출력될 것 같아요.
ans.add(String.format("%16s", Integer.toString(arr1[i] | arr2[i], 2)).replace("1", "#").replace("0", " ")); 로 하시면 될 것 같습니다.