algorithm
[c++] N!의 표현법, N!에서 0의 개수
tonirr
2020. 12. 10. 00:30
- N!의 표현법
#include <iostream>
#include <vector>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
//freopen("input.txt", "rt", stdin);
int n, i, j, tmp;
scanf("%d", &n);
std::vector <int> a(n+1);
for(i=2; i<=n; i++){
tmp=i;
j=2;
while(1){
if(tmp%j==0){
a[j]++;
tmp=tmp/j;
}
else {
j++;
}
if(tmp==1) break;
}
}
printf("%d! = ", n);
for(i=2; i<=n; i++){
if(a[i]!=0) printf("%d ", a[i]);
}
return 0;
}
- N!에서 0의 개수
#include <iostream>
#include <vector>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
//freopen("input.txt", "rt", stdin);
int n, i, j, tmp=1, cnt1=0, cnt2=0;
scanf("%d", &n);
for(i=2; i<=n; i++){
tmp=i;
j=2;
while(1){
if(tmp%j==0){
if(j==2) cnt1++;
if(j==5) cnt2++;
tmp=tmp/j;
}
else j++;
if(tmp==1) break;
}
}
if(cnt1<cnt2) printf("%d", cnt1);
if(cnt1>cnt2) printf("%d", cnt2);
}