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和 ...
随机推荐
- ch1_6_6求解门禁系统问题
import java.util.HashMap; import java.util.Scanner; public class ch1_6_6求解门禁系统问题 { public static voi ...
- Genymotion下载模拟器慢
•问题来源 Genymotion 是个很不错的 Android 模拟器,系统更新快,启动速度快: 但是服务器在国外,Android 镜像下载起来那个速度就不敢恭维了: 当然如果你可以[科学,上网]就另 ...
- 基于vite2的react脚手架
vite-react-boilerplate 开发编译 yarn start 启动开发 yarn build 启动编译 代码质量和风格 husky/lint-staged/eslint/prettie ...
- Chapter 2 简单DC-DC变换器稳态分析小结
Chapter 2 简单DC-DC变换器稳态分析小结 1 本章重点 1.1 小纹波近似 所谓小纹波近似就是DC-DC变换器的稳态分析中,假定开关频率次的纹波相对于直流分量而言非常小,可以将其忽略进行各 ...
- 腾讯高级工程师带你玩转打包利器webpack
随着前端领域飞速发展,webpack将前端不断出现的新模块.新资源.新需求,进行自动化整合.梳理.输出,极大提高了我们的工作效率,成为前端构建领域里最炙手可热的构建工具. 不少人webpack 的使用 ...
- 动态的创建Class对象方法及调用方式性能分析
有了Class对象,能做什么? 创建类的对象:调用Class对象的newInstance()方法 类必须有一个无参数的构造器. 类的构造器的访问权限需要足够. 思考?没有无参的构造器就不能创建对象吗? ...
- Java常用工具+类库合集
1 常用工具 JVisual vm:可以直接通过软件包下载,支持本地以及远程JVM监控 JMH:Java Microbenchmark Harness,测试基准组件,精度可达纳秒级 JITWatch: ...
- NumPy之:标量scalars
目录 简介 scalar类型的层次结构 内置Scalar类型 boolean Integers Unsigned integers Floating-point numbers Complex flo ...
- zabbix容器化安装及监控docker应用
一.zabbix agent2 介绍 从Zabbix 4.4之后,官方推出了Zabbix Agent 2,意味着zabbix 不在只是物理机监控的代名词,现在你可以使用Go为Zabbix编写插件,来监 ...
- 基于MVC框架的JavaWeb网站开发demo项目(JSP+Servlet+JavaBean)
1.环境配置 Windows10+Eclipse2020+jdk8+Tomcat9+MySQL8+Navicat10 2.需求分析 ①用户登录注册注销(查找.增加) ②显示用户列表(查找) ③显示用户 ...