Uva160——Factors and Factorials
来源:互联网 发布:跟淘宝一样的购物软件 编辑:程序博客网 时间:2024/06/06 09:43
求n!的质因子个数。也就是2~n每个数的质因子的个数之和。
输出的格式比较麻烦,需要注意。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int vis[110], prime[100], count = 0;int ans[100];void prime_table() //获取质数表{int i, j;memset(vis, 0, sizeof(vis));for(i = 2; i < 102; i++){if(!vis[i]){prime[count++] = i;for(j = 2 * i; j < 102; j += i)vis[j] = 1;}}}int main(){prime_table();int n;while(cin >> n){if(!n)break;memset(ans, 0, sizeof(ans));for(int i = 2; i <= n; i++) //对于每个i,求它的质因子个数{int m = i, now = 0;while(m > 1){while(m % prime[now] == 0){ans[now]++; //质因子加1m /= prime[now];}now++;}}printf("%3d! =", n);for(int j = 0; prime[j] <= n; j++){printf("%3d", ans[j]);if((j + 1) % 15 == 0)//每行输出15个,{printf("\n");if(prime[j + 1] <= n)//如果后面还有数,则需要输出相应的空行,printf(" ");}}if(prime[14] > n || prime[15] <= n)//判断是否需要换行,printf("\n");}return 0;}
0 0
- Uva160——Factors and Factorials
- uva160 Factors and Factorials
- JOJ1775:Factors and Factorials
- Factors and Factorials
- UVa 160 - Factors and Factorials
- UVa 160-Factors and Factorials
- UVa 160 Factors and Factorials
- UVa 160 Factors and Factorials (数论)
- UVa OJ 160 Factors and Factorials
- UVa 160 Factors ans Factorials(数论)
- Problem 34—Digit factorials
- Factorials and Trailing Zeroes
- factorials
- Factorials
- Factorials
- uva 129 krypton factors ——yhx
- uva 129 krypton factors ——yhx
- Problem 47—Distinct primes factors
- 变量、数据类型和运算符
- java小程序之(GUI)确认对话框JOptionPane.showConfirmDialog
- LeetCode 128. Longest Consecutive Sequence
- Dijkstra找最短路径
- 深入理解RunLoop
- Uva160——Factors and Factorials
- gym 100935B (字典树)
- Spring Bean的域scop
- HTML中img路径问题
- 虚幻4目录结构
- JAVA代码实现FTP文件下载
- GitHub 上排名前 100 的 Objective-C 项目简介
- 237. Delete Node in a Linked List
- 1008-I