AOJ 0052: Factorial II
問題
nの階乗を求めたとき、末尾に0がいくつ並ぶか
方針
10=2x5
であるので10をつくるには因数に2と5が必要。明らかに2の方がたくさんあるので5の個数を数える
コード
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) int main(void){ int n; while(cin >> n && n) { int ans = 0; for(int i=1; i<=n; ++i) { int tmp = i; while(tmp%5 == 0) { ans++; tmp /= 5; } } cout << ans << endl; } }
反省
while文を正しく書けなかった