UVA 160 - Factors and Factorials
Factors and Factorials |
The factorial of a number N (written N!) is defined as the product of all the integers from 1 to N. It is often defined recursively as follows:
Factorials grow very rapidly--5! = 120, 10! = 3,628,800. One way of specifying such large numbers is by specifying the number of times each prime number occurs in it, thus 825 could be specified as (0 1 2 0 1) meaning no twos, 1 three, 2 fives, no sevens and 1 eleven.
Write a program that will read in a number N ( ) and write out its factorial in terms of the numbers of the primes it contains.
Input
Input will consist of a series of lines, each line containing a single integer N. The file will be terminated by a line consisting of a single 0.
Output
Output will consist of a series of blocks of lines, one block for each line of the input. Each block will start with the number N, right justified in a field of width 3, and the characters `!', space, and `='. This will be followed by a list of the number of times each prime number occurs in N!.
These should be right justified in fields of width 3 and each line (except the last of a block, which may be shorter) should contain fifteen numbers. Any lines after the first should be indented. Follow the layout of the example shown below exactly.
Sample input
5
53
0
Sample output
5! = 3 1 1
53! = 49 23 12 8 4 4 3 2 2 1 1 1 1 1 1
1
#include <cstdio>
#include <cstring>
using namespace std;
bool isprime(int a)
{
int i;
for (i = 2; i*i <= a;i++)
if (a%i == 0)
return false;
return true;
}
int prime[100], count[100];
int main()
{
int n;
int i,num;
for (i = 2, num = 0; i <= 100;i++)
if (isprime(i))
{
prime[num++] = i;
} while (scanf("%d", &n) == 1 && n)
{
memset(count,0,sizeof(count));
int maxn = 0;
for (i = 2; i <= n; i++)
{
int m = i;
int j;
for (j = 0; j < num; j++)
{
while (m%prime[j] == 0)
{
m = m / prime[j];
count[j]++;
if (j>maxn)
maxn = j;
}
}
}
printf("%3d! =", n);
for (i = 0; i <= maxn; i++)
{
if (i == 15)
printf("\n ");
printf("%3d", count[i]);
} printf("\n");
}
return 0;
}
这道题应该特别注意输出格式。
最新文章
- java中的浮点数
- 连载《一个程序猿的生命周期》-《发展篇》 - 3.农民与软件工程师,农业与IT业
- 调整ESX的VMFS磁盘格式的块大小,让单个虚拟磁盘支持更大容量
- 电脑不能浏览网页but能登录qq,解决方案总结
- 使用datatable 将测试数据与业务分离
- Android 数独游戏 记录
- xtraScrollableControl 滚动条随鼠标滚动
- ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget
- 读书笔记 - 设计模式(Head First)
- python学习之 dictionary 、list、tuple操作
- 手写Maven的archetype项目脚手架
- java 重定向和转发的区别
- ADO.NET复习总结(1)--ADO.NET基础介绍
- (二十九)java条件控制语句培训笔记
- ubuntu ssh 防止登陆断开
- go语言中的运算符^,&;
- Java编程思想 学习笔记3
- GridLayout和GridView的区别
- 使用java修改图片DPI
- Oracle数据库采用数据泵方式导入导出数据
热门文章
- Java8的新特性--Optional
- 2019第十届蓝桥杯省赛及国赛个人总结(java-B组)
- 2021-2-28 Mark-Java Interview Simple
- 【Linux学习笔记0】-虚拟机运行CentOS(VMware12+CentOS)
- 【工程应用一】 多目标多角度的快速模板匹配算法(基于NCC,效果无限接近Halcon中........)
- OO第二单元作业——魔鬼电梯
- Spring Boot入门学习
- 《C标准库》学习笔记整理
- 【学习底层原理系列】重读spring源码3-加载beanDefinition的方法obtainFreshBeanFactory
- grafana接入zabbix数据源