Official

C - Unlucky 7 Editorial by en_translator


In order to write a number in base \(8\) or \(10\), the most popular and easiest way is to find the least significant digit first, and then calculate the more significant ones:

Sample Code (C++)

string to_oct(int n){
  string s;
  while(n){
    s = to_string(n%8) + s;
    n /= 8;
  }
  return s;
}

Note that some languages have standard functions or methods to convert to decimal or octal.

Sample Code (Python)

N = int(input())
ans = 0
for i in range(1, N+1):
  if ('7' not in str(i)) and ('7' not in oct(i)):
    ans += 1
print(ans)

Sample Code (Ruby)

N = gets.to_i
ans = 0
for i in 1..N do
  if not i.to_s(10).include?('7') and not i.to_s(8).include?('7') then
    ans += 1
  end
end
p ans

posted:
last update: