[C#] 002 - Encyclopedia of Parentheses
๐ ้ฃๆๅบฆ : โญโญโญ ๐ ่จ่ช : C#
๐ถ ่งฃใๆน
โ Bit Shiftใๅฉ็จใใ
using System;
using System.Text;
namespace atCoder.Contest
{
class Q_002_Encyclopedia_of_Parentheses
{
static void Main()
{
int N = Convert.ToInt32(Console.ReadLine());
if (N % 2 == 1) return;
// (1<<N)ๆผ็ฎ == 2^N
for (int i = 0; i < (1 << N); i++)
{
StringBuilder brackets = new StringBuilder();
for (int j = N - 1; j > -1; j--)
{
// i &(1<<j) ใฏใiใฎj็ช็ฎใฎใใใใ็ขบ่ชใใใใจ
if ((i & (1 << j)) == 0)
brackets.Append('(');
else
brackets.Append(')');
}
int count = 0;
for (int k = 0; k < brackets.Length; k++)
{
count += brackets[k] == '(' ? 1 : -1;
if (count < 0) break ;
}
if(count == 0)
Console.WriteLine(brackets);
}
}
}
}
(post-code: categories_sub_01)