Basic exercise factorial calculation of test questions
Problem description
Enter a positive integer n and output n! Value of.
Where n= 123*…*n.
Algorithm description
n! It may be very large, and the range of integers that can be represented by the computer is limited, so it is necessary to use high-precision calculation methods. An array A is used to represent a large integer a, A[0] represents one bit of a, A[1] represents ten bits of a, and so on.
Multiply a by an integer k to multiply each element of array a by K. please pay attention to handling the corresponding carry.
First set a to 1, then multiply 2, multiply 3, when multiplied to N, you get n! Value of.
Input format
The input contains a positive integer n, n < = 1000.
Output format
Output n! The exact value of.
sample input
10
sample output
3628800
#include <stdio.h> #define MAXNUMBER 100000 int main() { int n, i, j, temp; int a[MAXNUMBER] = {0}; a[0] = 1; scanf("%d",&n); for(i = 1; i <= n; ++i) { for(j = 0; j < MAXNUMBER ; ++j) { a[j] *= i; } for(j = 0; j < MAXNUMBER ; ++j) { if(a[j] > 9) { temp = a[j]; a[j] = a[j] % 10; temp = temp - a[j]; a[j + 1] += temp / 10; } } } for(j = MAXNUMBER - 1; j >= 0 ; --j) { if(a[j] != 0) break; } for(i = j; i >= 0; --i) { printf("%d",a[i]); } return 0; }
Basic exercise of test questions high precision addition
Problem description
Input two integers a and b and output the sum of these two integers. Both a and b have no more than 100 digits.
Algorithm description
Since both a and b are large, they cannot be stored directly using the standard data types in the language. For this problem, arrays are generally used.
Define an array a, A[0] is used to store the bits of a, A[1] is used to store the ten bits of a, and so on. You can also use an array B to store B.
When calculating c = a + b, first add A[0] and B[0]. If a carry is generated, store the carry (i.e. the ten digits of the sum) in R and the single digits of the sum in C[0], i.e. C[0] is equal to (A[0]+B[0])%10. Then calculate the addition of A[1] and B[1]. At this time, also add up the value r of the low order, that is, C[1] should be the sum of A[1], B[1] and R. if a carry is generated again, the new carry can still be stored in R and the bits of sum can be stored in C[1]. By analogy, all bits of C can be found.
Finally, output C.
Input format
The input consists of two lines, the first line is a non negative integer a, and the second line is a non negative integer b. Both integers have no more than 100 bits, and the highest bit of the two numbers > is not 0.
Output format
Output a line representing the value of a + b.
#include <stdio.h> int main() { int i=0, j=0; char num; int lena, lenb, max; int a[100] = {0}, b[100] = {0}; int c[100] = {0}, d[100] = {0}; int temp[100] = {0}; while(1) { scanf("%c",&num); if(num == '\n') break; else c[i++] = num - '0'; } num = 0; while(1) { scanf("%c",&num); if(num == '\n') break; else d[j++] = num - '0'; } lena = i; lenb = j; for(i = i - 1; i >= 0; --i) { a[lena - i - 1] = c[i]; } for(j = j - 1; j >= 0; --j) { b[lenb - j - 1] = d[j]; } max = lena > lenb ? lena : lenb; /*It is processed according to the data with the largest number of bits*/ for(i = 0; i < max; ++i) { if(temp[i] != 0) { a[i] += temp[i] / 10; } a[i] = a[i] + b[i]; if(a[i] > 9) { temp[i+1] = a[i] - a[i] % 10; a[i] = a[i] - temp[i+1]; } } if(temp[max] && max < 100) /*Also determine whether the highest bit will carry (the highest bit may reach 101)*/ printf("%d",temp[max]/10); for(i = max - 1; i >=0; --i) printf("%d",a[i]); return 0; }