int128 能够运算10000位的超大数据 我们的任务就 是:输入一个类型为int128的数字n(0<=n<=10^10001) 判断它是否为3的倍数。 輸入說明:輸入一個很大的數n 輸出說明:輸出 n!的末尾零的個數。用c语言实现
时间: 2024-03-20 13:42:55 浏览: 13
好的,下面是使用C语言实现判断一个int128类型的数字n!末尾零的个数的代码:
```
#include <stdio.h>
#include <string.h>
typedef struct {
int s[10000]; // 数字的每一位
int len; // 数字的位数
} int128;
void read_int128(int128 *a) {
char s[10002];
int i, k;
scanf("%s", s);
a->len = strlen(s);
k = 0;
for (i = a->len - 1; i >= 0; i--) {
a->s[++k] = s[i] - '0';
}
while (k > 1 && a->s[k] == 0) k--;
a->len = k;
}
void multiply_int128(int128 *a, int b) {
int i, carry = 0;
for (i = 1; i <= a->len; i++) {
int tmp = a->s[i] * b + carry;
a->s[i] = tmp % 10;
carry = tmp / 10;
}
while (carry) {
a->s[++a->len] = carry % 10;
carry /= 10;
}
}
int count_trailing_zeros(int128 a) {
int count = 0;
while (a.s[1] == 0) {
count++;
for (int i = 1; i < a.len; i++) {
a.s[i] = a.s[i + 1];
}
a.len--;
}
return count;
}
int main() {
int128 n, ans;
int i;
// 读入n
read_int128(&n);
// 计算n!
ans.s[1] = 1;
ans.len = 1;
for (i = 1; i <= n.len; i++) {
multiply_int128(&ans, n.s[i]);
}
// 计算末尾零的个数
printf("%d\n", count_trailing_zeros(ans));
return 0;
}
```
需要注意的是,由于int128类型的数字太大,需要使用高精度计算,因此代码的运行效率较低。此外,在计算n!的过程中,可能会出现int128类型的数乘以一个较大的数的情况,需要使用高精度乘法来进行计算。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)