打印平衡括号表达式
原文:
给定四个整数 a 、 b 、 c 和 d ,表示四种括号的数量。
- "(("
- "()"
- ")("
- "))"
任务是使用所有给定的括号打印任何平衡的括号表达式。如果我们不能形成一个平衡的括号表达式,那么打印 -1 。如果有多个答案,打印任意一个。 例:
输入: a = 3,b = 1,c = 4,d = 3 输出:((((((())())))))() 输入: a = 3,b = 1,c = 4,d = 8 输出: -1 给定的括号不可能有平衡的括号表达式。
方法:首先检查给定数量的括号是否能形成平衡括号表达式。如果类型 1 括号的数量等于类型 4 括号的数量,并且有任意数量的类型 3 括号,或者只有类型 2 括号,我们就可以形成表达式。因此组合条件为:
(a = = d & & a)| |(a = = 0 & & c = = 0 & & d = = 0)
如果满足以上条件,可以按照以下步骤打印平衡括号表达式:
- 打印第 1 类括号的数量。
- 打印 3 型括号的数量。
- 打印第 4 类括号的数量。
- 打印第 2 类括号的数量。
以下是上述方法的实现:
c
// c implementation of the approach
#include
using namespace std;
// function to print balanced bracket
// expression if it is possible
void printbalancedexpression(int a, int b, int c, int d)
{
// if the condition is met
if ((a == d && a) || (a == 0 && c == 0 && d == 0)) {
// print brackets of type-1
for (int i = 1; i <= a; i )
cout << "((";
// print brackets of type-3
for (int i = 1; i <= c; i )
cout << ")(";
// print brackets of type-4
for (int i = 1; i <= d; i )
cout << "))";
// print brackets of type-2
for (int i = 1; i <= b; i )
cout << "()";
}
// if the condition is not met
else
cout << -1;
}
// driver code
int main()
{
int a = 3, b = 1, c = 4, d = 3;
printbalancedexpression(a, b, c, d);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the above approach
class gfg
{
// function to print balanced bracket
// expression if it is possible
static void printbalancedexpression(int a,
int b, int c, int d)
{
// if the condition is met
if (((a == d) && (a != 0)) ||
((a == 0) && (c == 0) && (d == 0)))
{
// print brackets of type-1
for (int i = 1; i <= a; i )
system.out.print("((");
// print brackets of type-3
for (int i = 1; i <= c; i )
system.out.print(")(");
// print brackets of type-4
for (int i = 1; i <= d; i )
system.out.print("))");
// print brackets of type-2
for (int i = 1; i <= b; i )
system.out.print("()");
}
// if the condition is not met
else
system.out.print(-1);
}
// driver code
public static void main(string args[])
{
int a = 3, b = 1, c = 4, d = 3;
printbalancedexpression(a, b, c, d);
}
}
// this code is contributed by ryuga
python 3
# python3 implementation of the approach
# function to print balanced bracket
# expression if it is possible
def printbalancedexpression(a, b, c, d):
# if the condition is met
if ((a == d and a) or
(a == 0 and c == 0 and d == 0)):
# print brackets of type-1
for i in range(1, a 1):
print("((", end = "")
# print brackets of type-3
for i in range(1, c 1):
print(")(", end = "")
# print brackets of type-4
for i in range(1, d 1):
print("))", end = "")
# print brackets of type-2
for i in range(1, b 1):
print("()", end = "")
# if the condition is not met
else:
print("-1")
# driver code
if __name__ == "__main__":
a, b, c, d = 3, 1, 4, 3
printbalancedexpression(a, b, c, d)
# this code is contributed by rituraj jain
c
// c# implementation of the approach
using system;
class gfg
{
// function to print balanced bracket
// expression if it is possible
static void printbalancedexpression(int a,
int b, int c, int d)
{
// if the condition is met
if (((a == d) && (a != 0)) ||
((a == 0) && (c == 0) && (d == 0)))
{
// print brackets of type-1
for (int i = 1; i <= a; i )
console.write("((");
// print brackets of type-3
for (int i = 1; i <= c; i )
console.write(")(");
// print brackets of type-4
for (int i = 1; i <= d; i )
console.write("))");
// print brackets of type-2
for (int i = 1; i <= b; i )
console.write("()");
}
// if the condition is not met
else
console.write(-1);
}
// driver code
public static void main()
{
int a = 3, b = 1, c = 4, d = 3;
printbalancedexpression(a, b, c, d);
}
}
// this code is contributed by akanksha rai
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
output:
(((((()()()()())))))()
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处