aboutsummaryrefslogtreecommitdiff
path: root/max_69_number.c
blob: 40365b9a043e70508cf12587a1e4703327856ede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 1323. Maximum 69 Number
#include "leetcode.h"

/*
 * you are given a positive integer 'num' consisting only of digit 6 and 9.
 * return the maximum number you can get by changing at most one digit. (6
 * becomes 9, and 9 becomes 6).
 */

int maximum69Number(int num) {
  int right = -1, cnt = 0, tmp = num;
  while (tmp > 0) {
    int digits = tmp % 10;
    if (digits == 6)
      right = cnt;
    cnt++;
    tmp /= 10;
  }
  if (right == -1)
    return num;
  int ans = num + (3 * pow(10, right));
  return ans;
}

int main() {
  printf("%d\n", maximum69Number(9669)); // expect: 9969
  printf("%d\n", maximum69Number(9996)); // expect: 9999
  printf("%d\n", maximum69Number(9999)); // expect: 9999
}