입력으로 정수 숫자가 주어집니다. 목표는 제품에서 후행 0의 수를 찾는 것입니다. 11 X 22 X 33 X…X num num .
예를 들어
입력
num=5
출력
Count of number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. are: 5
설명
The number of 2s and 5s in the product will be: 11 * 22* 33* 44* 55=11 * 22* 33* (22)4* 55. So total 10 2s and 5 5s, minimum is 5 so trailing zeroes will be 5.
입력
num=10
출력
Count of number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. are: 5
설명
The number of 2s and 5s in the product will be: 11 *22*33*44*55*66 *77*88*99*1010 = 11 *22*33*44*55*66 *77*88*99*(2*5)10. So total 20 2s and 15 5s, minimum is 15 so trailing zeroes will be 15.
아래 프로그램에서 사용된 접근 방식은 다음과 같습니다. -
이 접근 방식에서 우리는 곱의 각 숫자의 소인수분해에서 2와 5의 수를 계산할 것입니다. 각 숫자가 고유한 거듭제곱으로 증가함에 따라 인수분해에서 2 또는 5의 최소 개수는 후행 0의 개수를 제공합니다. 각 2*5는 제품에 하나의 0을 추가합니다.
-
정수를 입력으로 받습니다.
-
count_trailing(int num) 함수는 num을 취하고 (1^1)*(2^2)*(3^3)*(4^4)*.....
에서 후행 0의 개수를 반환합니다. -
초기 카운트를 0으로 합니다.
-
변수 temp_2 =0, temp_5 =0을 2초와 5초로 셉니다.
-
for 루프를 사용하여 i=1에서 i<=num.
까지 순회 -
온도를 그대로 유지하십시오.
-
temp를 2로 나눌 수 있는 동안 반으로 줄이고 i를 추가하여 temp_2를 2로 계산합니다.
-
temp를 5로 나눌 수 있는 동안 5로 나누고 i를 추가하여 temp_5를 5로 계산합니다.
-
count =min(temp_2, temp_5)를 사용하여 최소 2개의 카운트로 계산합니다.
-
결과로 카운트를 반환합니다.
예시
#include <bits/stdc++.h>
using namespace std;
int count_trailing(int num){
int count = 0;
int temp_2 = 0;
int temp_5 = 0;
for (int i = 1; i <= num; i++){
int temp = i;
while(temp % 2 == 0 && temp > 0){
temp = temp / 2;
temp_2 = temp_2 + i;
}
while (temp % 5 == 0 && temp > 0){
temp = temp / 5;
temp_5 = temp_5+ i;
}
}
count = min(temp_2, temp_5);
return count;
}
int main(){
int num = 5;
cout<<"Count of number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. are: "<<count_trailing(num);
return 0;
} 출력
위의 코드를 실행하면 다음과 같은 출력이 생성됩니다 -
Count of number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. are: 5