原文:
给定两个整数 n 和 m ,其中 n 是 0s 的个数 m 是 1s 的个数。任务是将所有 0s 和 1s 打印在一行中,使得没有两个 0s 在一起,也没有三个 1s 在一起。如果无法根据情况排列 0s 和 1s ,则打印 -1 。 举例:
输入: n = 1,m = 2 输出: 011 输入: n = 4,m = 8 输出: 110110110101
方法:我们只有在((n–1)≤m和 m ≤ 2 * (n 1) 时才有答案。
- 如果(m = = n–1)则打印图案010101……从 0 开始,直到所有 0s 和 1s 都已使用。
- 如果 (m > n) 和 m ≤ 2 * (n 1) 则打印图案110110110……直到有多余的 1s 并在 m 变为等于n–1时变为图案0101010……。
以下是上述方法的实现:
c
// c implementation of the approach
#include
using namespace std;
// function to print the required pattern
void printpattern(int n, int m)
{
// when condition fails
if (m > 2 * (n 1) || m < n - 1) {
cout << "-1";
}
// when m = n - 1
else if (abs(n - m) <= 1) {
while (n > 0 && m > 0) {
cout << "01";
n--;
m--;
}
if (n != 0) {
cout << "0";
}
if (m != 0) {
cout << "1";
}
}
else {
while (m - n > 1 && n > 0) {
cout << "110";
m = m - 2;
n = n - 1;
}
while (n > 0) {
cout << "10";
n--;
m--;
}
while (m > 0) {
cout << "1";
m--;
}
}
}
// driver program
int main()
{
int n = 4, m = 8;
printpattern(n, m);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the above approach
class gfg
{
// function to print the required pattern
static void printpattern(int n, int m)
{
// when condition fails
if (m > 2 * (n 1) || m < n - 1)
{
system.out.print("-1");
}
// when m = n - 1
else if (math.abs(n - m) <= 1)
{
while (n > 0 && m > 0)
{
system.out.print("01");
n--;
m--;
}
if (n != 0)
{
system.out.print("0");
}
if (m != 0)
{
system.out.print("1");
}
}
else
{
while (m - n > 1 && n > 0)
{
system.out.print("110");
m = m - 2;
n = n - 1;
}
while (n > 0)
{
system.out.print("10");
n--;
m--;
}
while (m > 0)
{
system.out.print("1");
m--;
}
}
}
// driver code
public static void main(string []args)
{
int n = 4, m = 8;
printpattern(n, m);
}
}
// this code is contributed by ita_c.
python 3
# python 3 implementation of the approach
# function to print the required pattern
def printpattern(n, m):
# when condition fails
if (m > 2 * (n 1) or m < n - 1):
print("-1", end = "")
# when m = n - 1
elif (abs(n - m) <= 1):
while (n > 0 and m > 0):
print("01", end = "");
n -= 1
m -= 1
if (n != 0):
print("0", end = "")
if (m != 0):
print("1", end = "")
else:
while (m - n > 1 and n > 0):
print("110", end = "")
m = m - 2
n = n - 1
while (n > 0):
print("10", end = "")
n -= 1
m -= 1
while (m > 0):
print("1", end = "")
m -= 1
# driver code
if __name__ == '__main__':
n = 4
m = 8
printpattern(n, m)
# this code is contributed by
# surendra_gangwar
c
// c# implementation of the above approach
using system;
class gfg
{
// function to print the required pattern
static void printpattern(int n, int m)
{
// when condition fails
if (m > 2 * (n 1) || m < n - 1)
{
console.write("-1");
}
// when m = n - 1
else if (math.abs(n - m) <= 1)
{
while (n > 0 && m > 0)
{
console.write("01");
n--;
m--;
}
if (n != 0)
{
console.write("0");
}
if (m != 0)
{
console.write("1");
}
}
else
{
while (m - n > 1 && n > 0)
{
console.write("110");
m = m - 2;
n = n - 1;
}
while (n > 0)
{
console.write("10");
n--;
m--;
}
while (m > 0)
{
console.write("1");
m--;
}
}
}
// driver code
public static void main()
{
int n = 4, m = 8;
printpattern(n, m);
}
}
// this code is contributed by ryuga
服务器端编程语言(professional hypertext preprocessor 的缩写)
2 * ($n 1) || $m < $n - 1)
{
echo("-1");
}
// when m = n - 1
else if (abs($n - $m) <= 1)
{
while ($n > 0 && $m > 0)
{
system.out.print("01");
$n--;
$m--;
}
if ($n != 0)
{
echo("0");
}
if ($m != 0)
{
echo("1");
}
}
else
{
while ($m - $n > 1 && $n > 0)
{
echo("110");
$m = $m - 2;
$n = $n - 1;
}
while ($n > 0)
{
echo("10");
$n--;
$m--;
}
while ($m > 0)
{
echo("1");
$m--;
}
}
}
// driver code
$n = 4; $m = 8;
printpattern($n, $m);
// this code is contributed by
// mukul singh.
?>
java 描述语言
output
110110110101
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处