当前位置:网站首页>Factorial summation
Factorial summation
2022-06-21 14:47:00 【User 6978604】
Title source :C Language network 1014
Problem description
seek Sn=1!+2!+3!+4!+5!+…+n! It's worth , among n It's a number (n No more than 20).
The sample input
5Sample output
153problem solving
/*
* @Author: YaleXin
* @Date: 2020-05-22 20:40:31
* @LastEditTime: 2020-05-22 22:19:56
* @LastEditors: YaleXin
* @Description:
* @FilePath: \my_c_workspace\dotcpp\1014.c
* @ Prayer does not appear BUG
*/
#include <stdio.h>
#define MAX_NUM 100
typedef struct factorial {
int factorial[100];
} f;
f num[25];
int ans[100] = {0};
// High precision storage
void setNum(int index) {
if (index == 1) {
num[1].factorial[1] = 1;
return;
} else {
int c = 0, mul = 0, i;
for (i = 1;; i++) {
mul = (num[index - 1].factorial[i] * index + c) % MAX_NUM;
c = (num[index - 1].factorial[i] * index + c) / MAX_NUM;
num[index].factorial[i] = mul;
if ((mul + c + num[index - 1].factorial[i + 1]) == 0) break;
}
}
}
int addAns(int index) {
int c = 0, i, sum = 0;
for (i = 1;; i++) {
sum = (ans[i] + num[index].factorial[i] + c) % MAX_NUM;
c = (ans[i] + num[index].factorial[i] + c) / MAX_NUM;
ans[i] = sum;
if ((sum + c + ans[i + 1]) == 0) break;
}
return i;
}
int main() {
int n, i, len;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
setNum(i);
len = addAns(i);
}
for (i = len - 1; i >= 1; i--) {
// The first leading zero is removed
if (ans[i] < 10 && i != len - 1) {
printf("0%d", ans[i]);
} else {
// Single digit zero filling
printf("%d", ans[i]);
}
}
return 0;
}边栏推荐
- SSH based command operation
- Imitation B station web, app, background
- 2022 Hunan top eight (Safety Officer) simulated test question bank and answers
- Machine learning model training template
- Write a compile time annotation
- [untitled] fish pond forwarding command
- Is it safe to open a securities account by downloading the app of qiniu school? Is there a risk?
- Sliding validation tool class
- JS written test question: asynchronous
- 理财产品的赎回时间是怎么规定的?
猜你喜欢

Read distributed consistency protocols 2pc and 3pc

Application GDB debugging

Mqtt keepalive and reconnect

Win10 install tensorflow

Detailed explanation of dynamic planning

Win10 installation and configuration mongodb

Sliding validation tool class

First Canary deployment with rancher

Summary of the most basic methods of numpy

Dplayer development barrage background
随机推荐
Machine learning model training template
Record the processing process of slow response of primary system
Kubeneters installation encountered gcr The IO image warehouse cannot be pulled
Teach you to stop visiting a website
Redis5.0 installation and production startup steps
DP question brushing record
. bash_ profile
HSV color model and color component range in opencv
How is the redemption time of financial products stipulated?
Nmap scan port tool
Work assessment of contract law of Jilin University in March of the 22nd spring -00100
Chapter 4 - network layer
Usage of SED (replacement, deletion of text content, etc.)
Windows系统下C语言连接MySQL
Getting started with qt-1-ui
Kubeneters installation problems Collection
First Canary deployment with rancher
网上开户安全吗?新手可以开账户吗
Reverse generate the corresponding DTD constraint according to the XML file
Computer shortcuts During sorting, fill in as needed