当前位置:网站首页>Haut OJ 1245: large factorial of CDs --- high precision factorial
Haut OJ 1245: large factorial of CDs --- high precision factorial
2022-07-05 05:17:00 【hunziHang】
Problem description :
cds: I heard that you can already use C Language seeking n! 了
ykc: Of course ! Light yard oj I have already solved all the factorial problems AC 了 , for example 1048 Factorial table ,1050 The cumulative sum of factorials ,1089 The highest position of factorial, etc ……
cds: Oh ? Really? , Then I'll give you a number n, Can you find its factorial immediately ?
ykc: good , That's all right. !
cds: can n Very big
ykc: Don't worry , I use long long Just fine
cds: Good. ,n=80
ykc:&#¥%#woc
Input :
Single instance test , Enter a natural number n(n<=2000)
Output :
Output n The factorial
The sample input :
80
Sample output :
71569457046263802294811533723186532165584657342365752577109445058227039255480148842668944867280814080000000000000000000
Cause analysis :
1. utilize i To achieve 1 To n The factorial ,temp Record the number obtained by multiplying each time ,digit Record the number of digits ( So that each number can be multiplied )
2. temp=a[j]*i+num; num It is a number on a bit that multiplies and advances backward (num=temp/10;) The number on the bit that should be carried * i after , Need to add low and progressive num.
3. hold num/10 such as 4 The factorial And finally 4*(2*3), namely 24, here num=2 Save to digit That is to say 1 Location
Build another while Yes, it will appear later num There are several , So use a cycle to save each .
Solution :
#include <stdio.h>
int main()
{
int a[20001];// Store the number obtained by each bit
int temp,digit,n,i,j=0;//temp The number of each time digit The number of digits each time
scanf("%d",&n);
a[0]=1;// from 1 Start to multiply
digit=1;// The number of digits starts from the first
for(i=2;i<=n;i++)
{
int num=0;
for(j=0;j<digit;j++) //for Purpose : Multiply each digit of a number by i,
{
temp=a[j]*i+num;
a[j]=temp%10; // Each digit of a number is stored in an array
num=temp/10;
}
while(num)
{
a[digit]=num%10; // Continue to store the number after the last carry
// The previous carry is in the second for Stored in
num=num/10;
digit++;
}
}
for(i=digit-1;i>=0;i--)// Output each bit in reverse order
printf("%d",a[i]);
printf("\n");
return 0;
}
Multi instance input :
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll i,j,n;
while(cin>>n)
{
ll digit=1,a[100000]; //a Array ,a[0]=1,digit Need to put it in .
a[0]=1;
for(j=2;j<=n;j++)
{
ll num=0,temp;
for(i=0;i<digit;i++)
{
temp=a[i]*j+num;
a[i]=temp%10;
num=temp/10;
}
while(num)
{
a[digit]=num%10;
num/=10;
digit++;
}
}
for(i=digit-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
}
边栏推荐
- Lua GBK and UTF8 turn to each other
- [转]: OSGI规范 深入浅出
- National teacher qualification examination in the first half of 2022
- Data is stored in the form of table
- BUUCTF MISC
- The next key of win generates the timestamp file of the current day
- cocos2dx_ Lua card flip
- Simple modal box
- Animation
- [speed pointer] 142 circular linked list II
猜你喜欢
Optimization scheme of win10 virtual machine cluster
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
[turn]: OSGi specification in simple terms
stm32Cubemx(8):RTC和RTC唤醒中断
Chinese notes of unit particle system particle effect
用 Jmeter 工具做个小型压力测试
[depth first search] 695 Maximum area of the island
Reverse one-way linked list of interview questions
Do a small pressure test with JMeter tool
随机推荐
Lua determines whether the current time is the time of the day
Judge the position of the monster in the role under unity3d
Shell Sort
Basic knowledge points
Reverse one-way linked list of interview questions
Solon Logging 插件的添加器级别控制和日志器的级别控制
Collapse of adjacent vertical outer margins
使用命令符关闭笔记本自带键盘命令
C language Essay 1
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Fragment addition failed error lookup
Cocos progress bar progresstimer
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
[speed pointer] 142 circular linked list II
Learning notes of "hands on learning in depth"
对象的序列化
嵌入式数据库开发编程(五)——DQL
[allocation problem] 455 Distribute cookies
FVP和Juno平台的Memory Layout介绍
win下一键生成当日的时间戳文件