Given a string s
and a character letter
, return the percentage of characters in s
that equal letter
rounded down to the nearest whole percent.
Example 1:
Input: s = "foobar ", letter = "o " Output: 33 Explanation: The percentage of characters in s that equal the letter 'o' is 2 / 6 * 100% = 33% when rounded down, so we return 33.
Example 2:
Input: s = "jjjj ", letter = "k " Output: 0 Explanation: The percentage of characters in s that equal the letter 'k' is 0%, so we return 0.
Constraints:
1 <= s.length <= 100
s
consists of lowercase English letters.letter
is a lowercase English letter.program main
implicit none
character(len=*), parameter :: s = "foobar "
character, parameter :: letter = "o"
integer :: result
result = solve(s, letter)
print "(I0)", result
contains
function solve(s, letter) result(percentage)
implicit none
character(len=*), intent(in) :: s
character, intent(in) :: letter
integer :: i, n, percentage
n = len(s)
do i = 1, n
if (s(i:i) == letter) then
percentage = percentage + 1
end if
end do
percentage = percentage * 100 / n
percentage = int(percentage, kind=4)
end function solve
end program main
28
program main
implicit none
integer :: i, j, n, len
character(len=*) :: s, letter
real :: percentage
! Read input
read(*,*) s
read(*,*) letter
! Calculate percentage
len = len(s)
n = 0
do i = 1, len
if (s(i:i) == letter) then
n = n + 1
end if
end do
percentage = real(n) / real(len) * 100.0
percentage = int(percentage)
! Output result
write(*,*) percentage
end program main
temp.f95:13:7: 13 | len = len(s) | 1 Error: Unclassifiable statement at (1) temp.f95:5:35: 5 | character(len=*) :: s, letter | 1 Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER temp.f95:5:27: 5 | character(len=*) :: s, letter | 1 Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER
def three_consecutive_integers(num):
if num % 3 == 0:
return [num // 3 - 1, num // 3, num // 3 + 1]
elif num % 3 == 1:
return [(num - 2) // 3, (num + 1) // 3, (num + 4) // 3]
else:
return []
The algorithm first checks if the number num
is divisible by 3. If it is, the three consecutive integers must be {num / 3 - 1, num / 3, num / 3 + 1}
. If num
is one more than a multiple of 3 (i.e., num % 3 == 1
), then the three consecutive integers are {(num - 2) / 3, (num + 1) / 3, (num + 4) / 3}
. If num
is neither divisible by 3 nor one more than a multiple of 3 (i.e., num % 3 == 2
), it cannot be expressed as the sum of three consecutive integers, so the function returns an empty array.
#include <vector>
std::vector<int> threeConsecutiveIntegers(int num) {
if (num % 3 == 0) {
return {num / 3 - 1, num / 3, num / 3 + 1};
} else if (num % 3 == 1) {
return {(num - 2) / 3, (num + 1) / 3, (num + 4) / 3};
} else {
return {};
}
}
The algorithm first checks if the number num
is divisible by 3. If it is, the three consecutive integers must be {num / 3 - 1, num / 3, num / 3 + 1}
. If num
is one more than a multiple of 3 (i.e., num % 3 == 1
), then the three consecutive integers are {(num - 2) / 3, (num + 1) / 3, (num + 4) / 3}
. If num
is neither divisible by 3 nor one more than a multiple of 3 (i.e., num % 3 == 2
), it cannot be expressed as the sum of three consecutive integers, so the function returns an empty array.