Parentheses Matrix(构造)

Problem Description

A parentheses matrix is a matrix where every element is either '(' or ')'. We define the goodness of a parentheses matrix as the number of balanced rows (from left to right) and columns (from up to down). Note that:

- an empty sequence is balanced;
- if A is balanced, then (A) is also balanced;
- if A and B are balanced, then AB is also balanced.

For example, the following parentheses matrix is a 2×4 matrix with goodness 3, because the second row, the second column and the fourth column are balanced:

)()(
()()

Now, give you the width and the height of the matrix, please construct a parentheses matrix with maximum goodness.

Input

The first line of input is a single integer T (1≤T≤50), the number of test cases.

Each test case is a single line of two integers h,w (1≤h,w≤200), the height and the width of the matrix, respectively.

Output

For each test case, display h lines, denoting the parentheses matrix you construct. Each line should contain exactly w characters, and each character should be either '(' or ')'. If multiple solutions exist, you may print any of them.

Sample Input

3 1 1 2 2 2 3

Sample Output

( () )( ((( )))

 

构造题,遇事不决就打表。

需要打表找规律,才能找出题解的规律。

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
const double INF=0x3f3f3f3f;
char ma[220][220];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        memset(ma,' ',sizeof(ma));
        scanf("%d%d",&n,&m);
        if((n&1)&&(m&1))
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    printf("(");
                }
                printf("\n");
            }
        }
        if(!(n&1)&&(m&1))
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    if(i&1)printf("(");
                    else printf(")");
                }
                printf("\n");
            }
        }
        if((n&1)&&!(m&1)) 
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j+=2)
                {
                    printf("()");
                }
                printf("\n");
            }
        }
        if(!(n&1)&&!(m&1))//都是偶数
        {
            if(n<6||m<6) //特殊讨论
            {
                if(n<=m)
                {
                    for(int i=1; i<=n; i++)
                    {
                        for(int j=1; j<=m; j++)
                        {
                            if(i==1)ma[i][j]='(';
                            else if(i==n)ma[i][j]=')';
                            else if((i&1)==0)
                            {
                                if(j<=(m/2))ma[i][j]=')';
                                else ma[i][j]='(';
                            }
                            else
                            {
                                if(j<=(m/2))ma[i][j]='(';
                                else ma[i][j]=')';
                            }
                        }
                    }

                }
                else
                {
                    for(int i=1;i<=m;i++)
                    {
                        for(int j=1;j<=n;j++)
                        {
                            if(i==1)ma[j][i]='(';
                            else if(i==m)ma[j][i]=')';
                            else if((i&1)==0)
                            {
                                if(j<=(n/2))ma[j][i]=')';
                                else ma[j][i]='(';
                            }
                            else
                            {
                                if(j<=(n/2))ma[j][i]='(';
                                else ma[j][i]=')';
                            }
                        }
                    }
                }
                for(int i=1; i<=n; i++)
                {
                    for(int j=1; j<=m; j++)
                    {
                        printf("%c",ma[i][j]);
                    }
                    printf("\n");
                }
                continue;
            }
            for(int i=1; i<=m; i++)ma[1][i]='(';
            for(int i=1; i<=n; i++)ma[i][1]='(';
            for(int i=1; i<=m; i++)ma[n][i]=')';
            for(int i=2; i<=n; i++)ma[i][m]=')';
            for(int i=2; i<=n-1; i++)
            {
                for(int j=2; j<=m-1; j+=2)
                {
                    if((i&1)==0)
                    {
                        ma[i][j]=')';
                        ma[i][j+1]='(';
                    }
                    else
                    {
                        ma[i][j]='(';
                        ma[i][j+1]=')';
                    }
                }
            }
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    printf("%c",ma[i][j]);
                }
                printf("\n");
            }
        }
    }
    return 0;
}

 

### parentheses 的含义 在编程中,`parentheses`(括号)是一个非常重要的概念,用于表示分组、优先级控制以及函数调用等多种用途。以下是 `parentheses` 在不同场景中的具体意义: #### 1. 表达式分组 在数学表达式或逻辑表达式中,圆括号 `( )` 被用来指定操作顺序,改变默认的运算符优先级[^1]。例如,在以下代码片段中,加法先于乘法执行是因为括号改变了优先级。 ```javascript let result = (2 + 3) * 4; // 结果为 20 ``` #### 2. 函数定义与调用 在大多数编程语言中,圆括号被用来定义函数参数列表并调用函数[^2]。当创建一个函数时,括号内的内容称为 **parameters**;而在调用该函数时,传入的实际值则被称为 **arguments**。 ```javascript function greet(name) { console.log(`Hello, ${name}`); } greet('Alice'); // 输出 "Hello, Alice" ``` 此处: - `name` 是 parameter; - `'Alice'` 是 argument。 #### 3. 构造对象实例 某些面向对象的语言允许通过括号来初始化类的新实例。例如,在 Java 或 C++ 中可以这样写: ```java MyClass obj = new MyClass(); ``` 这里的小括号可能为空或者包含必要的初始配置项。 #### 4. 条件语句和循环结构 许多高级程序设计语言利用小括号包裹条件测试部分,比如 if 语句、while 循环等语法构建块里都会见到它们的身影[^3]。 ```python if (x > y): print("X is greater than Y.") for i in range(0, n): sum += i; ``` 以上 Python 示例展示了如何运用括号设定比较条件及迭代范围边界。 --- ### 总结 综上所述,`parentheses` 不仅限于简单的算术计算辅助工具角色,它还广泛应用于各种复杂的软件开发环节之中,包括但不限于过程抽象化封装、动态行为建模等方面发挥着不可替代的作用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值