OJ19

 1 // we have defined the necessary header files here for this problem.
 2 // If additional header files are needed in your program, please import here.
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <stdio.h>
 6 
 7 void add(const char *a, const char *b, char *res)
 8 {
 9     int ia[220000] = {0}, ib[220000] = {0}, ires[220000]= {0};
10     int la = strlen(a), lb = strlen(b);
11     for(int i = la - 1; i >= 0; i--)
12     {
13         ia[la - 1 - i] = a[i] - 48;
14     }
15     for(int i = lb - 1; i >= 0; i--)
16     {
17         ib[lb - 1 - i] = b[i] - 48;
18     }
19     int maxl = la > lb ? la : lb;
20     int resl = maxl; 
21     for(int i = 0; i < maxl; i++)
22     {
23         int sum = (ia[i] + ib[i] + ires[i]);
24         ires[i] = sum % 10;
25         ires[i + 1] = sum / 10;
26         if(i + 1 == maxl && ires[maxl] != 0)
27             resl++;
28     }
29     int first = resl - 1;
30     while(ires[first] == 0 && first != 0) first--;
31     for(int i = first; i >= 0; i--)
32     {
33         res[first - i] = ires[i] + 48;
34     }
35 }
36 char* multiply1(const char* a, const char b, char *ans)
37 {
38     for(int i = 0; i < b - 48; i++)
39     {
40         add(a, ans, ans);
41     }
42     return ans;
43 }
44 
45 char* multiplyn(const char *a, const char *b, char *ans)
46 {
47     char tempans[220000] = "0";
48     for(int i = strlen(b) - 1; i >= 0; i--)
49     {
50         char tempb[2000] = "";
51         char tempr[2000] = "0";
52         char zeros[2000] = "";
53         for(int z = 0; z < strlen(b) - 1 - i; z++)
54         {
55             zeros[z] = '0';
56         }
57         //memcpy()
58         // tempb[0] = b[i];
59         multiply1(a, b[i], tempr);
60         strcat(tempr, zeros);
61         add(tempans, tempr, tempans);
62     }
63     strcpy(ans,tempans);
64     return ans;
65 }
66 int main()
67 {
68     // please define the C input here. For example: int n; scanf("%d",&n);
69     // please finish the function body here. 
70     // please define the C output here. For example: printf("%d\n",a); 
71     
72     char ans[220000] = "1";
73     char is[6] = "";
74     int n;
75     while(scanf("%d", &n) != -1)
76     {
77         
78         for(int i = 2; i <= n; i++)
79         {
80             //itoa(i, is, 10);
81             sprintf(is, "%d", i);
82             multiplyn(ans, is, ans);
83             printf("%s\n", ans);
84         }
85         printf("%s\n", ans);
86         strcpy(ans, "1");
87     }
88 
89     return 0;
90 }

 

posted on 2023-02-07 17:18  SRnewcity  阅读(22)  评论(0编辑  收藏  举报

导航