You are given a positive integer num
consisting only of digits 6
and 9
.
Return the maximum number you can get by changing at most one digit (6
becomes 9
, and 9
becomes 6
).
Example 1:
Input: num = 9669 Output: 9969 Explanation: Changing the first digit results in 6669. Changing the second digit results in 9969. Changing the third digit results in 9699. Changing the fourth digit results in 9666. The maximum number is 9969.
Example 2:
Input: num = 9996 Output: 9999 Explanation: Changing the last digit 6 to 9 results in the maximum number.
Example 3:
Input: num = 9999 Output: 9999 Explanation: It is better not to apply any change.
Constraints:
1 <= num <= 104
num
consists of only 6
and 9
digits.program main
implicit none
integer, parameter :: num = 9669
integer :: max_number
max_number = solve(num)
print '(I0)', max_number
contains
function solve(n) result(max_n)
implicit none
integer, intent(in) :: n
integer :: max_n
integer :: i, j, k, l
do i = 1, len(n)
if (n(i:i) == '6') then
n(i:i) = '9'
max_n = max(max_n, n)
n(i:i) = '6'
else if (n(i:i) == '9') then
n(i:i) = '6'
max_n = max(max_n, n)
n(i:i) = '9'
end if
end do
end function solve
end program main
temp.f95:19:20: 19 | if (n(i:i) == '6') then | 1 Error: Syntax error in argument list at (1) temp.f95:23:25: 23 | else if (n(i:i) == '9') then | 1 Error: Syntax error in argument list at (1) temp.f95:27:15: 27 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:18:22: 18 | do i = 1, len(n) | 1 Error: βstringβ argument of βlenβ intrinsic at (1) must be CHARACTER
program maxNumber (
implicit none
integer :: num, max_num, i
character(len=1) :: digit
! read the input number
read(*,*) num
! initialize the maximum number
max_num = num
! loop through all digits of the number
do i = 1, len_trim(num)
digit = num(i:i)
! if the digit is a 6, change it to a 9
if (digit == '6') then
max_num = transfer(max_num, 9, i)
end if
end do
! print the maximum number
write(*,*) max_num
end program maxNumber
temp.f95:1:8: 1 | program maxNumber ( | 1 Error: Invalid form of PROGRAM statement at (1) temp.f95:26:21: 26 | end program maxNumber | 1 Error: Syntax error in END PROGRAM statement at (1) f951: Error: Unexpected end of file in βtemp.f95β
def maximum69Number(num: int) -> int:
num_str = list(str(num))
for i in range(len(num_str)):
if num_str[i] == '6':
num_str[i] = '9'
break
return int(''.join(num_str))
The algorithm to solve this problem is simple:
num
to a string, so that we can easily manipulate individual digits.This algorithm guarantees that only one digit will be modified at most, and the result will be the maximum value possible under the given constraints.
int maximum69Number (int num) {
string numStr = to_string(num);
for (char &c : numStr) {
if (c == '6') {
c = '9';
break;
}
}
return stoi(numStr);
}
The algorithm to solve this problem is simple:
num
to a string, so that we can easily manipulate individual digits.This algorithm guarantees that only one digit will be modified at most, and the result will be the maximum value possible under the given constraints.