-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
factll.c
115 lines (115 loc) · 1.99 KB
/
factll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
struct Node{
int data;
struct Node*next;
};
struct Node*head=NULL;
void createnode(struct Node**head)
{
struct Node*temp;
temp->next=*head;
*head=temp;
}
void push(struct Node**head,int new_data)
{
struct Node* new_node=(struct Node*)malloc(sizeof(struct Node));
new_node->data=new_data;
new_node->next=*head;
*head=new_node;
}
void printList(struct Node*node)
{
while(node!=NULL)
{
printf("%d",node->data);
node=node->next;
}
printf("\n");
}
void reverse(struct Node**head)
{
struct Node*pre=NULL;
struct Node*next=NULL;
struct Node*current=*head;
while(current!=NULL)
{
next=current->next;
current->next=pre;
pre=current;
current=next;
}
*head=pre;
}
int getSize(struct Node*node)
{
int size=0;
while(node!=NULL)
{
node=node->next;
size++;
}
return size;
}
struct Node*make_empty_list(int size)
{
struct Node*head=NULL;
while(size--)
{
push(&head,0);
}
return head;
}
int multiply(int *result,int x,int res_size)
{
int carry=0;
for(int i=0;i<res_size;i++)
{
carry=carry+result[i]*x;
result[i]=carry%10;
carry=carry/10;
}
while(carry)
{
result[res_size]=carry%10;
carry=carry/10;
res_size++;
}
return res_size;
}
void factorial(struct Node*head)
{
// int *result=malloc(10000*sizeof(char));
struct Node*result=NULL;
int res_size=1;
result[0]=1;
for(int x=2;x<=n;x++)
{
res_size=multiply(result,x,res_size);
}
for(int i=res_size-1;i>=0;i--)
{
printf("%d",result[i]);
}
free(result);
}
int main(void)
{
clock_t start,end;
double cpu_time_used;
start=clock();
char*num=malloc(255*sizeof(char));
scanf("%s",num);
for(int i=sizeof(num)-1;i>=0;i++)
{
push(&head,num[i]-'0');
}
factorial(&head);
end=clock();
free(num);
cpu_time_used=((double)(end-start))/CLOCKS_PER_SEC;
printf("\n%lf",cpu_time_used);
return 0;
}