A valid cut in a circle can be:
Some valid and invalid cuts are shown in the figures below.
Given the integer n
, return the minimum number of cuts needed to divide a circle into n
equal slices.
Example 1:
Input: n = 4 Output: 2 Explanation: The above figure shows how cutting the circle twice through the middle divides it into 4 equal slices.
Example 2:
Input: n = 3 Output: 3 Explanation: At least 3 cuts are needed to divide the circle into 3 equal slices. It can be shown that less than 3 cuts cannot result in 3 slices of equal size and shape. Also note that the first cut will not divide the circle into distinct parts.
Constraints:
1 <= n <= 100
program main
implicit none
integer :: n
print '(A)', 'Enter the value of n:'
read *, n
print '(I0)', minCuts(n)
contains
function minCuts(n) result(cuts)
implicit none
integer, intent(in) :: n
integer :: cuts
if (n == 1) then
cuts = 0
else if (n == 2) then
cuts = 1
else
cuts = 2
end if
end function minCuts
end program main
Enter the value of n:
At line 7 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7f70299d8960 in ??? #1 0x7f70299d94d9 in ??? #2 0x7f7029c2d17b in ??? #3 0x7f7029c26684 in ??? #4 0x7f7029c272aa in ??? #5 0x568e04e3c2e2 in MAIN__ #6 0x568e04e3c3c1 in main
PROGRAM main
IMPLICIT NONE
INTEGER :: n, i, j, k, l, m, n_cuts
REAL :: x1, y1, x2, y2, x3, y3, x4, y4
READ(*,*) n
! Initialize the number of cuts to 0
n_cuts = 0
! Check if n is valid
IF (n < 1 .OR. n > 100) THEN
WRITE(*,*) "Invalid input"
STOP
END IF
! Check if n is 1
IF (n == 1) THEN
WRITE(*,*) "0"
STOP
END IF
! Check if n is 2
IF (n == 2) THEN
WRITE(*,*) "1"
STOP
END IF
! Check if n is 3
IF (n == 3) THEN
WRITE(*,*) "2"
STOP
END IF
! Check if n is 4
IF (n == 4) THEN
WRITE(*,*) "3"
STOP
END IF
! Check if n is 5
IF (n == 5) THEN
WRITE(*,*) "4"
STOP
END IF
! Check if n is 6
IF (n == 6) THEN
WRITE(*,*) "5"
STOP
END IF
! Check if n is 7
IF (n == 7) THEN
WRITE(*,*) "6"
STOP
END IF
! Check if n is 8
IF (n == 8) THEN
WRITE(*,*) "7"
STOP
END IF
! Check if n is 9
IF (n == 9) THEN
WRITE(*,*) "8"
STOP
END IF
! Check if n is 10
IF (n == 10) THEN
WRITE(*,*) "9"
STOP
END IF
! Check if n is 11
IF (n == 11) THEN
WRITE(*,*) "10"
STOP
END IF
! Check if n is 12
IF (n == 12) THEN
WRITE(*,*) "11"
STOP
END IF
! Check if n is 13
IF (n == 13) THEN
WRITE(*,*) "12"
STOP
END IF
! Check if n is 14
IF (n == 14) THEN
WRITE(*,*) "13"
STOP
END IF
! Check if n is 15
IF (n == 15) THEN
WRITE(*,*) "14"
STOP
END IF
! Check if n is 16
IF (n == 16) THEN
WRITE(*,*) "15"
STOP
END IF
! Check if n is 17
IF (n == 17) THEN
WRITE(*,*) "16"
STOP
END IF
! Check if n is 18
IF (n == 18) THEN
WRITE(*,*) "17"
STOP
END IF
! Check if n is 19
IF (n == 19) THEN
WRITE(*,*) "18"
STOP
END IF
! Check if n is 20
IF (n == 20) THEN
WRITE(*,*) "19"
STOP
END IF
! Check if n is 21
IF (n == 21) THEN
WRITE(*,*) "20"
STOP
END IF
! Check if n is 22
IF (n == 22) THEN
W
temp.f95:146:10: 146 | W | 1 Error: Unclassifiable statement at (1) f951: Error: Unexpected end of file in βtemp.f95β
def min_cuts(n):
if n % 2 == 0:
return n // 2
else:
return n
If `n` is an even number, then each cut divides the circle into two equal slices, and thus the minimum number of cuts required is `n / 2`. If `n` is an odd number, then each cut divides the circle into one equal slice, and thus the minimum number of cuts required is `n`.
int min_cuts(int n) {
if (n % 2 == 0)
return n / 2;
else
return n;
}
If `n` is an even number, then each cut divides the circle into two equal slices, and thus the minimum number of cuts required is `n / 2`. If `n` is an odd number, then each cut divides the circle into one equal slice, and thus the minimum number of cuts required is `n`.