C++ 程序查找数字阶乘中的第一个数字
在本文中,我们将讨论一个程序,该程序用于查找给定数字阶乘中的第一个数字。
基本的方法是找出这个数字的阶乘,然后得到它的第一个数字。但是由于阶乘最终可能变得太大,所以我们会进行一些微小的调整。
在每个点上,我们都会检查是否存在尾随零,并删除如果存在的话。由于尾随零对第一个数字没有影响,因此我们的结果不会改变。
示例
#include <bits/stdc++.h>
using namespace std;
int calc_1digit(int n) {
long long int fact = 1;
for (int i = 2; i <= n; i++) {
fact = fact * i;
//removing trailing zeroes
while (fact % 10 == 0)
fact = fact / 10;
}
//finding the first digit
while (fact >= 10)
fact = fact / 10;
return fact;
}
int main() {
int n = 37;
cout << "The first digit : " << calc_1digit(n) << endl;
return 0;
}输出
The first digit : 1
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP