Calculate 1! +2! +3! + ····· + N!

zhaozj2021-02-16  195

Problem Description: Precise calculation 1! 2! 3! ··· N! 0

Algorithm Description :

1. Define two integer one-dimensional array TEMP [100] and SUM [100] .Temp [100] and SUM [100] each element is 0; the individual elements of the two arrays correspond to one large integer. 2. TMP [0] is assigned to 1.

3. Circulating variable I from 1 to N;

3.1 Multiply each element of TEMP [100];

3.2 Each element of TEMP adds the corresponding element of the SUM, and then imparts the inlet processing for the corresponding element 3.3 of the SUM.

4. Output. 4.1 Find the highest bit 4.2 output

Source code: (with c)

INCLUDE int main () {int N, sum [100], temp [100]; int I, k; printf ("Enter a number:"); scanf ("% d", & n); for (i = 0; i <= 99; i ) {SUM [i] = 0, TEMP [i] = 0;} TEMP [0] = 1; for (i = 1; i <= n; i ) {for (k = 0; k <= 99; k ) {TEMP [K] = Temp [k] * i;} for (k = 0; k <= 99; k ) {SUM [K] = TEMP [K] SUM [k];} for (k = 0; k <= 98; k ) {if (SUM [K]> = 10) {SUM [K 1] = SUM [K 1] (SUM [K] / 10); SUM [K] = SUM [K]% 10; / * inlet processing * /} if (Temp [K]> = 10) {TEMP [K 1] = Temp [K 1] (Temp [k] / 10); TEMP [K] = TEMP [K]% 10 ;; / * carry processing * /}}}} i = 99; for (i = 99; i> = 0; i -) { IF (SUM [I] == 0) Continue; Else Break;} / * Find the highest bit * / do {printf ("% d", sum [i]); i -;} while (i> = 0) PRINTF ("/ n");} / * Output * /

转载请注明原文地址:https://www.9cbs.com/read-8606.html

New Post(0)