Given a positive integer num
, return the number of positive integers less than or equal to num
whose digit sums are even.
The digit sum of a positive integer is the sum of all its digits.
Example 1:
Input: num = 4 Output: 2 Explanation: The only integers less than or equal to 4 whose digit sums are even are 2 and 4.
Example 2:
Input: num = 30 Output: 14 Explanation: The 14 integers less than or equal to 30 whose digit sums are even are 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.
Constraints:
1 <= num <= 1000
program main
implicit none
integer :: num, i, j, count
character(len=10) :: str
! read input
read(*,*) num
! solve problem
count = 0
do i = 1, num
write(str, '(I10)') i
j = 0
do while (j < len_trim(str))
j = j + 1
if (mod(ichar(str(j:j)), 2) == 0) then
count = count + 1
end if
end do
end do
! print output
write(*,'(I0)') count
end program main
At line 8 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7bc8ac98d960 in ??? #1 0x7bc8ac98e4d9 in ??? #2 0x7bc8acbe217b in ??? #3 0x7bc8acbdb684 in ??? #4 0x7bc8acbdc2aa in ??? #5 0x5c44dd07d21a in MAIN__ #6 0x5c44dd07d3fa in main
```
program solve_problem
implicit none
integer :: num, i, j, digit_sum
integer, dimension(1000) :: numbers
read *, num
! Initialize the array with all numbers up to num
do i = 1, num
numbers(i) = i
end do
! Loop through the array and remove all numbers with odd digit sums
do i = 1, num
digit_sum = 0
do j = 1, i
digit_sum = digit_sum + mod(i, 10)
end do
if (mod(digit_sum, 2) == 1) then
numbers(i) = 0
end if
end do
! Count the number of remaining numbers
count = 0
do i = 1, num
if (numbers(i) /= 0) then
count = count + 1
end if
end do
! Print the result
write (*,*) count
end program solve_problem
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:37:2: 37 | ``` | 1 Error: Invalid character in name at (1) temp.f95:26:9: 26 | count = 0 | 1 Error: Symbol โcountโ at (1) has no IMPLICIT type
def max_tasks(tasks, workers, pills, strength):
tasks.sort()
workers.sort()
task_index = 0
pill_index = pills
completed_tasks = 0
for worker_strength in workers:
while pill_index > 0 and worker_strength + strength >= tasks[task_index]:
worker_strength += strength
pill_index -= 1
if worker_strength >= tasks[task_index]:
task_index += 1
completed_tasks += 1
if task_index >= len(tasks):
break
return completed_tasks
1. First, we sort both the tasks and the workers arrays in increasing order.
#include <vector>
#include <algorithm>
int maxTasks(std::vector<int>& tasks, std::vector<int>& workers, int pills, int strength) {
std::sort(tasks.begin(), tasks.end());
std::sort(workers.begin(), workers.end());
int taskIndex = 0;
int pillIndex = pills;
int completedTasks = 0;
for (int workerStrength : workers) {
while (pillIndex > 0 && workerStrength + strength >= tasks[taskIndex]) {
workerStrength += strength;
--pillIndex;
}
if (workerStrength >= tasks[taskIndex]) {
++taskIndex;
++completedTasks;
}
if (taskIndex >= tasks.size()) {
break;
}
}
return completedTasks;
}
1. First, we sort both the tasks and the workers arrays in increasing order.