Algorithm
Problem Name: beecrowd | 3342
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/3342
Keanu
By Eliton Machado, UDESC Brazil
Timelimit: 1
Keanu was testing new chessboard models when he had the following doubt:
How many white squares and many black squares does an nxn-sized chessboard have?
3x3-sized chessboard:
5 white squares and 4 black squares
4x4-sized chessboard:
8 white squares and 8 black squares
Note that the topmost and leftmost square is always white.
Input
The input consists of a line with a single integer n.
2
n
100
Output
Print "a white squares and b black squares" without quotes, where a is the number of white squares and b the number of black squares.
Input Samples | Output Samples |
3 |
5 casas brancas e 4 casas pretas |
4 |
8 casas brancas e 8 casas pretas |
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("node:fs")
const [num] = readFileSync("/dev/stdin", "utf8")
.split("\n", 1)
.map(value => Number.parseInt(value, 10))
const whites = Math.ceil(Math.pow(num, 2) / 2)
const blacks = Math.floor(Math.pow(num, 2) / 2)
console.log("%d casas brancas e %d casas pretas", whites, blacks)
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto){ return lines.shift()}
var numero=Number(prompt());
var quadrado=numero**2;
if(quadrado%2==0){
var valor=quadrado/2;
console.log(valor+' casas brancas e '+valor+' casas pretas')
}else{
var valor=(quadrado-1)/2;
console.log((valor+1)+' casas brancas e '+valor+' casas pretas')
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
def pretobranco(n,tot):
if n%2==0:return tot//2,tot//2
return (tot+1)//2,((tot+1)//2)-1
n=int(input())
b,p=pretobranco(n,n*n)
print(f"{b} casas brancas e {p} casas pretas")
Copy The Code &
Try With Live Editor
Input
Output