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;
}
这道题应该特别注意输出格式。
UVA 160 - Factors and Factorials的更多相关文章
- UVA 1575 Factors
https://vjudge.net/problem/UVA-1575 题意: 令f(k)=n 表示 有n种方式,可以把正整数k表示成几个数的乘积的形式. 例 10=2*5=5*2,所以f(10)=2 ...
- [UVA160]Factors and Factorials 题解
前言 这道题目本身毫无技术含量珂言,但是输出格式珂以调一年 题解 这道题让我们求\(N!\)中每个质数的个数. 一种方法是直接模拟,枚举\(N!\)中的每个元素,然后暴力查看每个数含有有多少质数. 但 ...
- Zerojudge解题经验交流
题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- \r\n和\n的区别
写Java代码的时候习惯用\r\n换行,这样可移植性比较好但是,在UVa - 160中就出现了错误,来看看是为什么吧. http://bbs.csdn.net/topics/220033879
- UVA 10699 Count the factors 题解
Time limit 3000 ms OS Linux Write a program, that computes the number of different prime factors in ...
- uva 129 krypton factors ——yhx
Krypton Factor You have been employed by the organisers of a Super Krypton Factor Contest in which ...
- UVa 884 - Factorial Factors
题目:输出n!中素数因数的个数. 分析:数论.这里使用欧拉筛法计算素数,在计算过程中求解就可以. 传统筛法是利用每一个素数,筛掉自己的整数倍: 欧拉筛法是利用当前计算出的全部素数,乘以当前数字筛数: ...
- UVa 11621 - Small Factors
称号:发现没有比给定数量少n的.只要2,3一个因素的数字组成. 分析:数论.贪婪,分而治之. 用两个三分球,分别代表乘法2,和繁殖3队列,队列产生的数字,原来{1}. 然后.每取两个指针相应元素*2和 ...
随机推荐
- Spring源码之注解扫描Component-scan
本文主要介绍Spring的component-scan标签,了解spring是如何实现扫描注解进行bean的注册,主要实现实在 NamespaceHandler, NamespaceHandlerSu ...
- C语言之预处理详解
C语言之预处理详解 纲要: 预定义符号 #define #define定义标识符 #define定义宏 #define的替换规则 #与## 几点注意#undef 带副作用的宏参数 宏和函数的对比 命名 ...
- 敏捷史话(十二):你现在接触的敏捷也许是“黑暗敏捷”——Ron Jeffries
他很少提起往事,也不再提及二十年前那场引起软件行业变革的会议,他专注于当下,一直活跃在敏捷领域.八十多岁的他依然运营维护着网站和博客,是极限编程网站 XProgramming.com 的作者,该网站是 ...
- CentOS7 搭建 K8S 环境
前期准备 环境规划 K8S 与Docker兼容问题 k8s v1.18.0 => Docker v18.x k8s v1.19.0 => Docker v19.x 软件 版本 Linux操 ...
- Python数据分析入门(十四):数据分析中常用图
折线图: 折线图用于显示数据在一个连续的时间间隔或者时间跨度上的变化,它的特点是反映事物随时间或有序类别而变化的趋势.示例图如下: 折线图应用场景: 折线图适合X轴是一个连续递增或递减的,对于没有规律 ...
- 实际使用Elasticdump工具对Elasticsearch集群进行数据备份和数据还原
文/朱季谦 目录 一.Elasticdump工具介绍 二.Elasticdump工具安装 三.Elasticdump工具使用 最近在开发当中做了一些涉及到Elasticsearch映射结构及数据导出导 ...
- 带你全面认识CMMI V2.0(二)
CMMI V2.0成熟度等级 CMMI V2.0的一大变化是,所有实践领域均适用于成熟度三级(ML3),并具有特定的附加必需实践水平. 例如,在ML3上需要进行因果分析和解决,但在CMMI成熟度四级( ...
- C# WebView2 在你的应用中使用Chromium内核
什么是WebView2? Win10上对标Edge浏览器 Chromium内核 简单的可视为WebBrowser组件的升级版 如何使用WebView2? 官网下载 WebView2 RunTime V ...
- redhat7.6 更换 centos7 YUM
使用yum 遇到如下错误. This system is not registered to Red Hat Subscription Management. You can use subscrip ...
- Maven相关知识总结
目录 认识Maven Maven下载安装 Maven能用来做什么 Maven核心概念 开发目录 坐标和仓库 POM文件 POM文件内容 Maven依赖管理 构建生命周期 构建多模块系统 聚合 继承 聚 ...