N*N 棋盘上可以放置的最大主教数
给定一个整数n ,任务是打印可以放置在nxn棋盘上的最大主教数,以便没有两个主教互相攻击。例如,最多 2 位主教可以安全地放置在 2 x 2 棋盘上。
例子:
Input: n = 2
Output: 2
We can place two bishop in a row.
Input: n = 5
Output: 8
方法:主教可以在四个对角线中的任何一个移动。因此,如果主教不在另一位主教的任何对角线上,我们可以放置主教。可以放置在n * n棋盘上的最大象数为2 * (n – 1) 。
- 将 n 位主教放在第一行
- 将 n-2 位主教放在最后一行。我们只留下最后一排的两个角
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum number of bishops
// that can be placed on an n * n chessboard
int numberOfBishops(int n)
{
if (n < 1)
return 0;
else if (n == 1)
return 1;
else
return 2 * (n - 1);
}
// Driver code
int main()
{
int n = 5;
cout << numberOfBishops(n);
return 0;
}
Java
// Java implementation of the approach
class gfg
{
// Function to return the maximum
// number of bishops that can be
// placed on an n * n chessboard
static int numberOfBishops(int n)
{
if (n < 1)
return 0;
else if (n == 1)
return 1;
else
return 2 * (n - 1);
}
// Driver code
public static void main(String[] args)
{
int n = 5;
System.out.println(numberOfBishops(n));
}
}
// This code is contributed by Mukul Singh.
Python3
# Python3 implementation of the
# approach
import math as mt
# Function to return the maximum number
# of bishops that can be placed on an
# n * n chessboard
def numberOfBishops(n):
if (n < 1):
return 0
elif (n == 1):
return 1
else:
return 2 * (n - 1)
# Driver code
n = 5
print(numberOfBishops(n))
# This code is contributed by
# Mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum number
// of bishops that can be placed on an
// n * n chessboard
static int numberOfBishops(int n)
{
if (n < 1)
return 0;
else if (n == 1)
return 1;
else
return 2 * (n - 1);
}
// Driver code
public static void Main()
{
int n = 5;
Console.Write(numberOfBishops(n));
}
}
// This code is contributed
// by Akanksha Rai
PHP
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the difference of
// two big numbers as string
string subtract(string str1, string str2)
{
string res = "";
int n1 = str1.length();
int n2 = str2.length();
// To make subtraction easy
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
for (int i = 0; i < n2; i++) {
// Subtract digit by bdigit
int subst = ((str1[i] - '0')
- (str2[i] - '0') - carry);
if (subst < 0) {
subst = subst + 10;
carry = 1;
}
else
carry = 0;
// Change subst as character and
// add it to result string
res.push_back(subst + '0');
}
for (int i = n2; i < n1; i++) {
int subst = ((str1[i] - '0') - carry);
if (subst < 0) {
subst = subst + 10;
carry = 1;
}
else
carry = 0;
res.push_back(subst + '0');
}
// Reverse result to make it actual number
reverse(res.begin(), res.end());
return res;
}
string NumberOfBishops(string a)
{
if (a == "1")
return a;
else {
// Subtract 1 from number
a = subtract(a, "1");
// Reverse the string to make calculations easier
reverse(a.begin(), a.end());
int carry = 0;
// Multiply by 2
for (int i = 0; i < a.size(); i++) {
int tmp = a[i] - '0';
tmp *= 2;
tmp += carry;
a[i] = '0' + (tmp % 10);
carry = tmp / 10;
}
if (carry > 0)
a += ('0' + carry);
// Reverse the string to get actual result
reverse(a.begin(), a.end());
// Return result
return a;
}
}
// Driver code
int main()
{
string a = "12345678901234567890";
cout << NumberOfBishops(a) << endl;
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG{
public static char[] reverse(char []str)
{
char[] temp = new char[str.length];
// Fill character array backwards with
// characters of the string
for(int i = 0; i < str.length; i++)
temp[str.length - i - 1] = str[i];
// Convert character array to string
// and return it
return temp;
}
// Function to return the difference of
// two big numbers as String
static char[] subtract(char[] str1, char[] str2)
{
String res = "";
int n1 = str1.length;
int n2 = str2.length;
// To make subtraction easy
str1 = reverse(str1);
str2 = reverse(str2);
int carry = 0;
for(int i = 0; i < n2; i++)
{
// Subtract digit by bdigit
int subst = ((str1[i] - '0') -
(str2[i] - '0') - carry);
if (subst < 0)
{
subst = subst + 10;
carry = 1;
}
else
carry = 0;
// Change subst as character and
// add it to result String
res = res + (subst);
}
for(int i = n2; i < n1; i++)
{
int subst = ((str1[i] - '0') - carry);
if (subst < 0)
{
subst = subst + 10;
carry = 1;
}
else
carry = 0;
res += (subst);
}
// Reverse result to make it actual number
char[] Res = res.toCharArray();
Res = reverse(Res);
return Res;
}
static char[] NumberOfBishops(char[] a)
{
if (new String(a) == "1")
return a;
else
{
// Subtract 1 from number
a = subtract(a, "1".toCharArray());
//Console.WriteLine(new String(a));
// Reverse the String to make
// calculations easier
a = reverse(a);
int carry = 0;
// Multiply by 2
for(int i = 0; i < a.length; i++)
{
int tmp = a[i] - '0';
tmp *= 2;
tmp += carry;
a[i] = (char)('0' + (tmp % 10));
carry = tmp / 10;
}
String A = new String(a);
if (carry > 0)
A += ('0' + carry);
char[] a1 = A.toCharArray();
// Reverse the String to get
// actual result
a1 = reverse(a1);
// Return result
return a1;
}
}
// Driver code
public static void main(String []args)
{
char[] a = "12345678901234567890".toCharArray();
System.out.println(new String(NumberOfBishops(a)));
}
}
// This code is contributed by pratham76
Python3
# Python3 implementation of the approach
# Function to return the difference
# of two big numbers as string
def subtract(str1, str2):
res = ""
n1 = len(str1)
n2 = len(str2)
# To make subtraction easy,
# reverse the strings
str1 = str1[::-1]
str2 = str2[::-1]
carry = 0
for i in range(0, n2):
# Subtract digit by bdigit
subst = int(str1[i]) - int(str2[i]) - carry
if subst < 0:
subst = subst + 10
carry = 1
else:
carry = 0
# Change subst as character and
# add it to result string
res += str(subst)
for i in range(n2, n1):
subst = int(str1[i]) - carry
if subst < 0:
subst = subst + 10
carry = 1
else:
carry = 0
res += str(subst)
# Reverse result to make it
# actual number
return res[::-1]
def NumberOfBishops(a):
if a == "1":
return a
else:
# Subtract 1 from number
a = subtract(a, "1")
carry = 0
# Reverse the string to make
# calculations easier. Convert the
# string to list to manipulate it
# as strings are immutable in python
a = list(a[::-1])
# Multiply by 2
for i in range(0, len(a)):
tmp = (int(a[i]) * 2) + carry
a[i] = str(tmp % 10)
carry = tmp // 10
# Convert the list back to string
a = ''.join(a)
if carry > 0:
a += str(carry)
# Reverse the string to get
# actual result
return a[::-1]
# Driver code
if __name__ == "__main__":
a = "12345678901234567890"
print(NumberOfBishops(a))
# This code is contributed
# by Rituraj Jain
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the difference of
// two big numbers as string
static char[] subtract(char[] str1, char[] str2)
{
string res = "";
int n1 = str1.Length;
int n2 = str2.Length;
// To make subtraction easy
Array.Reverse(str1);
Array.Reverse(str2);
int carry = 0;
for (int i = 0; i < n2; i++)
{
// Subtract digit by bdigit
int subst = ((str1[i] - '0') - (str2[i] - '0') - carry);
if (subst < 0)
{
subst = subst + 10;
carry = 1;
}
else
carry = 0;
// Change subst as character and
// add it to result string
res = res + (subst);
}
for (int i = n2; i < n1; i++)
{
int subst = ((str1[i] - '0') - carry);
if (subst < 0)
{
subst = subst + 10;
carry = 1;
}
else
carry = 0;
res += (subst);
}
// Reverse result to make it actual number
char[] Res = res.ToCharArray();
Array.Reverse(Res);
return Res;
}
static char[] NumberOfBishops(char[] a)
{
if (new string(a) == "1")
return a;
else
{
// Subtract 1 from number
a = subtract(a, "1".ToCharArray());
//Console.WriteLine(new string(a));
// Reverse the string to make calculations easier
Array.Reverse(a);
int carry = 0;
// Multiply by 2
for (int i = 0; i < a.Length; i++)
{
int tmp = a[i] - '0';
tmp *= 2;
tmp += carry;
a[i] = (char)('0' + (tmp % 10));
carry = tmp / 10;
}
string A = new string(a);
if (carry > 0)
A += ('0' + carry);
char[] a1 = A.ToCharArray();
// Reverse the string to get actual result
Array.Reverse(a1);
// Return result
return a1;
}
}
// Driver code
static void Main()
{
char[] a = "12345678901234567890".ToCharArray();
Console.WriteLine(new string(NumberOfBishops(a)));
}
}
// This code is contributed by divyeshrabadiy07
Javascript
输出:
8
下面是更大的n值的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the difference of
// two big numbers as string
string subtract(string str1, string str2)
{
string res = "";
int n1 = str1.length();
int n2 = str2.length();
// To make subtraction easy
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
for (int i = 0; i < n2; i++) {
// Subtract digit by bdigit
int subst = ((str1[i] - '0')
- (str2[i] - '0') - carry);
if (subst < 0) {
subst = subst + 10;
carry = 1;
}
else
carry = 0;
// Change subst as character and
// add it to result string
res.push_back(subst + '0');
}
for (int i = n2; i < n1; i++) {
int subst = ((str1[i] - '0') - carry);
if (subst < 0) {
subst = subst + 10;
carry = 1;
}
else
carry = 0;
res.push_back(subst + '0');
}
// Reverse result to make it actual number
reverse(res.begin(), res.end());
return res;
}
string NumberOfBishops(string a)
{
if (a == "1")
return a;
else {
// Subtract 1 from number
a = subtract(a, "1");
// Reverse the string to make calculations easier
reverse(a.begin(), a.end());
int carry = 0;
// Multiply by 2
for (int i = 0; i < a.size(); i++) {
int tmp = a[i] - '0';
tmp *= 2;
tmp += carry;
a[i] = '0' + (tmp % 10);
carry = tmp / 10;
}
if (carry > 0)
a += ('0' + carry);
// Reverse the string to get actual result
reverse(a.begin(), a.end());
// Return result
return a;
}
}
// Driver code
int main()
{
string a = "12345678901234567890";
cout << NumberOfBishops(a) << endl;
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG{
public static char[] reverse(char []str)
{
char[] temp = new char[str.length];
// Fill character array backwards with
// characters of the string
for(int i = 0; i < str.length; i++)
temp[str.length - i - 1] = str[i];
// Convert character array to string
// and return it
return temp;
}
// Function to return the difference of
// two big numbers as String
static char[] subtract(char[] str1, char[] str2)
{
String res = "";
int n1 = str1.length;
int n2 = str2.length;
// To make subtraction easy
str1 = reverse(str1);
str2 = reverse(str2);
int carry = 0;
for(int i = 0; i < n2; i++)
{
// Subtract digit by bdigit
int subst = ((str1[i] - '0') -
(str2[i] - '0') - carry);
if (subst < 0)
{
subst = subst + 10;
carry = 1;
}
else
carry = 0;
// Change subst as character and
// add it to result String
res = res + (subst);
}
for(int i = n2; i < n1; i++)
{
int subst = ((str1[i] - '0') - carry);
if (subst < 0)
{
subst = subst + 10;
carry = 1;
}
else
carry = 0;
res += (subst);
}
// Reverse result to make it actual number
char[] Res = res.toCharArray();
Res = reverse(Res);
return Res;
}
static char[] NumberOfBishops(char[] a)
{
if (new String(a) == "1")
return a;
else
{
// Subtract 1 from number
a = subtract(a, "1".toCharArray());
//Console.WriteLine(new String(a));
// Reverse the String to make
// calculations easier
a = reverse(a);
int carry = 0;
// Multiply by 2
for(int i = 0; i < a.length; i++)
{
int tmp = a[i] - '0';
tmp *= 2;
tmp += carry;
a[i] = (char)('0' + (tmp % 10));
carry = tmp / 10;
}
String A = new String(a);
if (carry > 0)
A += ('0' + carry);
char[] a1 = A.toCharArray();
// Reverse the String to get
// actual result
a1 = reverse(a1);
// Return result
return a1;
}
}
// Driver code
public static void main(String []args)
{
char[] a = "12345678901234567890".toCharArray();
System.out.println(new String(NumberOfBishops(a)));
}
}
// This code is contributed by pratham76
蟒蛇3
# Python3 implementation of the approach
# Function to return the difference
# of two big numbers as string
def subtract(str1, str2):
res = ""
n1 = len(str1)
n2 = len(str2)
# To make subtraction easy,
# reverse the strings
str1 = str1[::-1]
str2 = str2[::-1]
carry = 0
for i in range(0, n2):
# Subtract digit by bdigit
subst = int(str1[i]) - int(str2[i]) - carry
if subst < 0:
subst = subst + 10
carry = 1
else:
carry = 0
# Change subst as character and
# add it to result string
res += str(subst)
for i in range(n2, n1):
subst = int(str1[i]) - carry
if subst < 0:
subst = subst + 10
carry = 1
else:
carry = 0
res += str(subst)
# Reverse result to make it
# actual number
return res[::-1]
def NumberOfBishops(a):
if a == "1":
return a
else:
# Subtract 1 from number
a = subtract(a, "1")
carry = 0
# Reverse the string to make
# calculations easier. Convert the
# string to list to manipulate it
# as strings are immutable in python
a = list(a[::-1])
# Multiply by 2
for i in range(0, len(a)):
tmp = (int(a[i]) * 2) + carry
a[i] = str(tmp % 10)
carry = tmp // 10
# Convert the list back to string
a = ''.join(a)
if carry > 0:
a += str(carry)
# Reverse the string to get
# actual result
return a[::-1]
# Driver code
if __name__ == "__main__":
a = "12345678901234567890"
print(NumberOfBishops(a))
# This code is contributed
# by Rituraj Jain
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the difference of
// two big numbers as string
static char[] subtract(char[] str1, char[] str2)
{
string res = "";
int n1 = str1.Length;
int n2 = str2.Length;
// To make subtraction easy
Array.Reverse(str1);
Array.Reverse(str2);
int carry = 0;
for (int i = 0; i < n2; i++)
{
// Subtract digit by bdigit
int subst = ((str1[i] - '0') - (str2[i] - '0') - carry);
if (subst < 0)
{
subst = subst + 10;
carry = 1;
}
else
carry = 0;
// Change subst as character and
// add it to result string
res = res + (subst);
}
for (int i = n2; i < n1; i++)
{
int subst = ((str1[i] - '0') - carry);
if (subst < 0)
{
subst = subst + 10;
carry = 1;
}
else
carry = 0;
res += (subst);
}
// Reverse result to make it actual number
char[] Res = res.ToCharArray();
Array.Reverse(Res);
return Res;
}
static char[] NumberOfBishops(char[] a)
{
if (new string(a) == "1")
return a;
else
{
// Subtract 1 from number
a = subtract(a, "1".ToCharArray());
//Console.WriteLine(new string(a));
// Reverse the string to make calculations easier
Array.Reverse(a);
int carry = 0;
// Multiply by 2
for (int i = 0; i < a.Length; i++)
{
int tmp = a[i] - '0';
tmp *= 2;
tmp += carry;
a[i] = (char)('0' + (tmp % 10));
carry = tmp / 10;
}
string A = new string(a);
if (carry > 0)
A += ('0' + carry);
char[] a1 = A.ToCharArray();
// Reverse the string to get actual result
Array.Reverse(a1);
// Return result
return a1;
}
}
// Driver code
static void Main()
{
char[] a = "12345678901234567890".ToCharArray();
Console.WriteLine(new string(NumberOfBishops(a)));
}
}
// This code is contributed by divyeshrabadiy07
Javascript
输出:
24691357802469135778
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。