Official

C - Unlucky 7 Editorial by kyopro_friends


数を \(10\) 進法や \(8\) 進法で表すには、下の桁から求めていく方法が最も有名かつ簡単です。

実装例(C++)

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

なお、\(10\) 進数や \(8\) 進数などに変換する関数・メソッドなどが標準で用意されている言語もあります。

実装例(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)

実装例(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: