A confusing number is a number that when rotated 180
degrees becomes a different number with each digit valid.
We can rotate digits of a number by 180
degrees to form new digits.
0
, 1
, 6
, 8
, and 9
are rotated 180
degrees, they become 0
, 1
, 9
, 8
, and 6
respectively.2
, 3
, 4
, 5
, and 7
are rotated 180
degrees, they become invalid.Note that after rotating a number, we can ignore leading zeros.
8000
, we have 0008
which is considered as just 8
.Given an integer n
, return true
if it is a confusing number, or false
otherwise.
Example 1:
Input: n = 6 Output: true Explanation: We get 9 after rotating 6, 9 is a valid number, and 9 != 6.
Example 2:
Input: n = 89 Output: true Explanation: We get 68 after rotating 89, 68 is a valid number and 68 != 89.
Example 3:
Input: n = 11 Output: false Explanation: We get 11 after rotating 11, 11 is a valid number but the value remains the same, thus 11 is not a confusing number
Constraints:
0 <= n <= 109
program main
implicit none
integer :: i, n
logical :: confusing
! Examples
do i = 1, size(examples)
read (examples(i), *) n
write (*, '(A, I0, A)', advance='no') 'Confusing number ', n, '? '
confusing = confusing_number(n)
if (confusing) then
write (*, '(A)') 'Yes'
else
write (*, '(A)') 'No'
end if
end do
contains
function confusing_number(n) result(confusing)
implicit none
integer, intent(in) :: n
logical :: confusing
integer :: rotated
confused = .false.
! Rotate the digits of n by 180 degrees
rotated = modulo(n * 10 + 10 - n, 100)
! Check if rotated is a valid number
select case (rotated)
case (0, 1, 6, 8, 9)
confusing = .true.
case default
confusing = .false.
end select
! Ignore leading zeros
rotated = abs(rotated)
! Check if rotated is equal to n
if (rotated == n) then
confusing = .false.
end if
end function confusing_number
end program main
integer, parameter :: examples(3) = (/ 6, 89, 11 /)
temp.f95:1:12: 1 | program main | 1 ...... 52 | integer, parameter :: examples(3) = (/ 6, 89, 11 /) | 2 Error: Two main PROGRAMs at (1) and (2)
program confusing_number
implicit none
integer :: n, rotated_n, digit, i
logical :: is_confusing
! Read the input number
read (*, *) n
! Initialize the is_confusing flag
is_confusing = .false.
! Rotate the number by 180 degrees
rotated_n = n
do i = 1, 180
rotated_n = rotated_n * 10
digit = mod(rotated_n, 10)
if (digit == 0 .or. digit == 1 .or. digit == 6 .or. digit == 8 .or. digit == 9) then
rotated_n = rotated_n / 10
else
is_confusing = .true.
exit
end if
end do
! Check if the rotated number is a valid number
if (is_confusing) then
! Check if the rotated number is different from the original number
if (rotated_n /= n) then
print *, "True"
else
print *, "False"
end if
else
print *, "False"
end if
end program confusing_number
This program uses a loop to rotate the input number by 180 degrees. It checks if each digit after rotation is valid (i.e., it is either 0, 1, 6, 8, or 9) and if it is not, it sets the is_confusing flag to true. If the is_confusing flag is true, it checks if the rotated number is different from the original number and prints "True" if it is, and "False" otherwise. If the is_confusing flag is false, it prints "False".
Note that this program assumes that the input number is non-negative. If the input number can be negative, you will need to add a check for this case.
temp.f95:40:132: 40 | This program uses a loop to rotate the input number by 180 degrees. It checks if each digit after rotation is valid (i.e., it is either 0, 1, 6, 8, or 9) and if it is not, it sets the is_confusing flag to true. If the is_confusing flag is true, it checks if the rotated number is different from the original number and prints "True" if it is, and "False" otherwise. If the is_confusing flag is false, it prints "False". | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:42:132: 42 | Note that this program assumes that the input number is non-negative. If the input number can be negative, you will need to add a check for this case. | 1 Error: Line truncated at (1) [-Werror=line-truncation] f951: some warnings being treated as errors
function shipWithinDays(weights, days) {
let left = Math.max(...weights), right = weights.reduce((a, b) => a + b, 0);
while (left < right) {
let mid = left + ((right - left) / 2) | 0;
let day_count = 1, curr_capacity = 0;
for (const w of weights) {
curr_capacity += w;
if (curr_capacity > mid) {
day_count++;
curr_capacity = w;
}
}
if (day_count > days) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
The algorithm employs binary search to find the least weight capacity. It starts by setting the initial search range to the maximum weight among packages (left) and the total weight of all packages (right). Then, the algorithm does the following steps until left is not less than right: 1. Compute the middle value (mid) of the search range. 2. Go through the packages' weights and count the days needed to ship them with the current mid value as the capacity. If the accumulated weight of packages exceeds the current capacity (mid), increment the day count and reset the accumulated weight. 3. If the calculated days needed is greater than the given days, we need a bigger capacity, so we update left to mid + 1. Otherwise, update right to mid.
After the loop, left is the least weight capacity that will ship all packages within days days.
#include <vector>
#include <algorithm>
int shipWithinDays(std::vector<int>& weights, int days) {
int left = *max_element(weights.begin(), weights.end());
int right = accumulate(weights.begin(), weights.end(), 0);
while (left < right) {
int mid = left + (right - left) / 2;
int day_count = 1, curr_capacity = 0;
for (int w : weights) {
curr_capacity += w;
if (curr_capacity > mid) {
day_count++;
curr_capacity = w;
}
}
if (day_count > days) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
The algorithm employs binary search to find the least weight capacity. It starts by setting the initial search range to the maximum weight among packages (left) and the total weight of all packages (right). Then, the algorithm does the following steps until left is not less than right: 1. Compute the middle value (mid) of the search range. 2. Go through the packages' weights and count the days needed to ship them with the current mid value as the capacity. If the accumulated weight of packages exceeds the current capacity (mid), increment the day count and reset the accumulated weight. 3. If the calculated days needed is greater than the given days, we need a bigger capacity, so we update left to mid + 1. Otherwise, update right to mid.
After the loop, left is the least weight capacity that will ship all packages within days days.