[프로그래머스] 비밀지도
Algorithm 2023. 1. 29. 10:34https://school.programmers.co.kr/learn/courses/30/lessons/17681
1단계
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Numerics;
using System.Text;
namespace BOJ
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
int n = int.Parse(sr.ReadLine());
//testcase 입력
for (int t = 0; t < 2; t++)
{
int[] arr1 = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);
// 5
// 9 20 28 18 11
// 30 1 21 17 28
for (int i = 0; i < arr1.Length; i++)
{
//convert.tostring : 10진수를 2진수로 변경
//padleft : n자리로 만들고 '0'을 채움
string binary = Convert.ToString(arr1[i], 2).PadLeft(n, '0');
sw.WriteLine(binary);
}
sw.WriteLine();
}
sr.Close();
sw.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Numerics;
using System.Text;
// 5
// 9 20 28 18 11
// 30 1 21 17 28
namespace BOJ
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
int n = int.Parse(sr.ReadLine());
int[] arr1 = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
int[] arr2 = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
string[] map1 = new string[n];
string[] map2 = new string[n];
for (int i = 0; i <n; i++)
{
map1[i] = Convert.ToString(arr1[i], 2).PadLeft(n, '0');
sw.Write(map1[i]);
sw.Write("\t");
map2[i] = Convert.ToString(arr2[i], 2).PadLeft(n, '0');
sw.Write(map2[i]);
sw.WriteLine();
}
List<string> answer = new List<string>();
for(int i = 0; i < n; i++)
{
var num1 = Convert.ToByte(map1[i], 2);
var num2 = Convert.ToByte(map2[i], 2);
var or = Convert.ToString(num1 | num2, 2).PadLeft(n, '0');
StringBuilder sb = new StringBuilder();
for (int j = 0; j < or.Length; j++)
{
sw.Write(or[j]);
if (or[j] == '0')
sb.Append(" ");
else
sb.Append("#");
}
sw.WriteLine(sb.ToString());
//sw.WriteLine();
answer.Add(sb.ToString());
}
// 6
// 46 33 33 22 31 50
// 27 56 19 14 14 10
foreach (var x in answer)
{
//sw.WriteLine(x);
}
sr.Close();
sw.Close();
}
}
}
c++
#include <string>
#include <vector>
using namespace std;
vector<string> solution(int n, vector<int> arr1, vector<int> arr2) {
vector<string> answer;
for(int i = 0; i<n; i++){
arr1[i] = arr1[i] | arr2[i];
string ans = "";
for(int j = 0; j<n; j++){
if(arr1[i] % 2 == 0)
ans = " " + ans;
else
ans = "#" + ans;
arr1[i] = arr1[i] >> 1;
}
answer.push_back(ans);
}
return answer;
}
'Algorithm' 카테고리의 다른 글
[BOJ/C#] 2864 5와 6의 차이 (0) | 2023.01.31 |
---|---|
[BOJ/C#] 10162 전자레인지 (0) | 2023.01.31 |
[BOJ/C#] 2163 초콜릿 자르기 (0) | 2023.01.29 |
[BOJ/C#] 3009 네 번째 점 (0) | 2023.01.28 |
[BOJ/C#] 2579 계단오르기 (0) | 2023.01.28 |