zoukankan      html  css  js  c++  java
  • 高精度模板(含加减乘除四则运算)

    高精度加高精度

     1 void BigAddBig(char *a, char *b, char *c)
     2 {
     3     //a表示结果,b,c位加数
     4     int a_int[1005] = { 0 }, b_int[1005] = { 0 }, c_int[1005] = { 0 };
     5     int len1, len2, len, i;
     6     len1 = strlen(b);
     7     len2 = strlen(c);
     8     for (i = 0; i < len1; i++)
     9         b_int[i] = b[len1 - 1 - i] - '0';
    10     for (i = 0; i<len2; i++)
    11         c_int[i] = c[len2 - 1 - i] - '0';
    12     len = len1>len2 ? len1 : len2;
    13     for (i = 0; i<len; i++)
    14     {
    15         a_int[i] += b_int[i] + c_int[i];
    16         if (a_int[i]>9)
    17         {
    18             a_int[i + 1] = a_int[i] / 10;
    19             a_int[i] = a_int[i] % 10;
    20         }
    21     }
    22     if (a_int[i] != 0)
    23         len++;
    24     while (!a_int[len - 1])
    25         len--;
    26     for (i = 0; i < len; i++)
    27         a[i] = a_int[len - 1 - i] + '0';
    28     a[i] = '';
    29 }
    View Code

    高精度减高精度(未经过严格测试)

     1 void BigSubBig(char *a, char *b, char *c)
     2 {
     3     //a表示结果,b被减数,c减数,不能得到负数,需要进入函数之前判断
     4     int a_int[1005] = { 0 }, b_int[1005] = { 0 }, c_int[1005] = { 0 };
     5     int len1, len2, len, i, owe;
     6     len1 = strlen(b);
     7     len2 = strlen(c);
     8     for (i = 0; i < len1; i++)
     9         b_int[i] = b[len1 - 1 - i] - '0';
    10     for (i = 0; i<len2; i++)
    11         c_int[i] = c[len2 - 1 - i] - '0';
    12     len = len1>len2 ? len1 : len2;
    13     owe = 0;
    14     for (i = 0; i < len; i++)
    15     {
    16         a_int[i] = b_int[i] - c_int[i] - owe;
    17         owe = 0;
    18         if (a_int[i] < 0)
    19         {
    20             owe = 1;
    21             a_int[i] = a_int[i] + 10;
    22         }
    23     }
    24     while (a_int[len - 1] == 0 && len != 1)
    25         len--;
    26     for (i = 0; i < len; i++)
    27         a[i] = a_int[len - 1 - i] + '0';
    28     a[i] = '';
    29 }
    View Code

    高精度乘以低精度

     1 void BigNumMultiSmall(char *a, char *b, int mul)
     2 {
     3     //a表示结果,b表示被乘数,mul表示乘数
     4     int i, j, len;
     5     int a_int[2000] = { 0 }, b_int[1000] = { 0 };
     6     len = strlen(b);
     7     for (i = 0; i < len; i++)
     8         b_int[i] = b[len - 1 - i] - '0';
     9     for (i = 0; i<len; i++)
    10     {
    11         a_int[i] = a_int[i] + b_int[i] * mul;
    12         if (a_int[i]>9)
    13         {
    14             a_int[i + 1] = a_int[i] / 10;
    15             a_int[i] = a_int[i] % 10;
    16         }
    17     }
    18     while (a_int[i])
    19     {
    20         a_int[i + 1] = a_int[i] / 10;
    21         a_int[i] = a_int[i] % 10;
    22         i++;
    23     }
    24     while (a_int[i - 1] == 0)
    25         i--;
    26     for (j = 0; j < i; j++)
    27         a[j] = a_int[i - j - 1] + '0';
    28     a[j] = '';
    29 }
    View Code

    高精度乘以高精度

     1 void BigMultiBig(char *a, char *b, char *c)
     2 {
     3     int i, j, len1, len2, len;
     4     int a_int[2010] = { 0 }, b_int[1000] = { 0 }, c_int[1000] = { 0 };
     5 
     6     len1 = strlen(b);
     7     for (i = len1 - 1; i >= 0; i--)
     8         b_int[len1 - i - 1] = b[i] - '0';
     9     len2 = strlen(c);
    10     for (i = len2 - 1; i >= 0; i--)
    11         c_int[len2 - i - 1] = c[i] - '0';
    12 
    13     len = len1 + len2;
    14 
    15     for (i = 0; i < len1; i++)
    16     for (j = 0; j < len2; j++)
    17         a_int[i + j] += b_int[i] * c_int[j];
    18     for (i = 0; i<len; i++)
    19     if (a_int[i]>9)
    20     {
    21         a_int[i + 1] += a_int[i] / 10;
    22         a_int[i] = a_int[i] % 10;
    23     }
    24     while (a_int[len - 1] == 0)
    25         len--;
    26 
    27     for (i = 0; i < len; i++)
    28         a[i] = a_int[len - i - 1] + '0';
    29     a[i] = '';
    30     if (strlen(a) == 0)
    31         strcpy(a, "0");
    32 }
    View Code

    高精度除以低精度

     1 int BigNumDividSmall(char *a, char *b, int div)
     2 {
     3     //z是余数,a是结果,b是被除数,div是除数
     4     int i, a_int[1000] = { 0 }, b_int[1000] = { 0 };
     5     int len = strlen(b);
     6     for (i = 0; i < len; i++)
     7         b_int[i] = b[i] - '0';
     8 
     9     int z = 0;
    10     for (i = 0; i < len; i++)
    11     {
    12         z = b_int[i] + z * 10;
    13         a_int[i] = z / div;
    14         z = z%div;
    15     }
    16 
    17     int t = 0;
    18     while (t < 1000 && !a_int[t])
    19         t++;
    20     if (t == 1000)
    21     {
    22         a[0] = '0';
    23         a[1] = '';
    24     }
    25     else
    26     {
    27         for (i = 0; i < len - t; i++)
    28             a[i] = a_int[i + t] + '0';
    29         a[i] = '';
    30     }
    31     return z;
    32 }
    View Code

    高精度除以高精度(未经过严格测试)

      1 #include<stdio.h>
      2 #include<string.h>
      3 void BigSubBig(char *a, char *b, char *c)
      4 {
      5     //a表示结果,b被减数,c减数,不能得到负数,需要进入函数之前判断
      6     int a_int[1005] = { 0 }, b_int[1005] = { 0 }, c_int[1005] = { 0 };
      7     int len1, len2, len, i, owe;
      8     len1 = strlen(b);
      9     len2 = strlen(c);
     10     for (i = 0; i < len1; i++)
     11         b_int[i] = b[len1 - 1 - i] - '0';
     12     for (i = 0; i<len2; i++)
     13         c_int[i] = c[len2 - 1 - i] - '0';
     14     len = len1>len2 ? len1 : len2;
     15     owe = 0;
     16     for (i = 0; i < len; i++)
     17     {
     18         a_int[i] = b_int[i] - c_int[i] - owe;
     19         owe = 0;
     20         if (a_int[i] < 0)
     21         {
     22             owe = 1;
     23             a_int[i] = a_int[i] + 10;
     24         }
     25     }
     26     while (a_int[len - 1] == 0 && len != 1)
     27         len--;
     28     for (i = 0; i<len; i++)
     29         a[i] = a_int[len - 1 - i] + '0';
     30     a[i] = '';
     31 }
     32 int compare(char *a, char *b)
     33 {
     34     int len1 = strlen(a);
     35     int len2 = strlen(b);
     36     if (len1>len2)
     37         return 1;
     38     if (len1 < len2)
     39         return -1;
     40     return strcmp(a, b);
     41 }
     42 void BigNumDividBig(char *a, char *arr, char *b, char *c)
     43 {
     44 
     45     char temp[1005], time[1005];
     46     int j, lentime, len, i, flag, k;
     47     if (compare(b, c) == -1)
     48     {
     49         a[0] = '0';
     50         a[1] = '';
     51         strcpy(arr, b);
     52     }
     53     else if (compare(b, c) == 0)
     54     {
     55         a[0] = '1';
     56         a[1] = '';
     57         arr[0] = '0';
     58         arr[1] = '';
     59     }
     60     else
     61     {
     62         j = lentime = 0;
     63         len = strlen(b);
     64         memset(temp, 0, sizeof(temp));
     65         memset(time, 0, sizeof(time));
     66         for (i = 0; i < len; i++)
     67         {//计算得到被除数的前几位,得到整型数组形式的商
     68             //time的一个元素表示一次相除的商
     69             temp[j++] = b[i];
     70             flag = 0;
     71             while (compare(temp, c) >= 0)
     72             {
     73                 BigSubBig(temp, temp, c);
     74                 time[lentime]++;
     75                 flag = 1;//控制time的元素的位置
     76             }
     77             if (flag)//将商转换为字符
     78                 time[lentime] += '0';
     79             else//当被除数前几位小于除数,商补    
     80                 time[lentime] = '0';
     81 
     82             if (!strcmp(temp, "0"))//若temp为''    
     83                 j = 0;
     84             else//继续在b的后面加值    
     85                 j = strlen(temp);
     86             lentime++;
     87         }
     88         k = 0;
     89         for (i = 0; i < lentime; i++)
     90         if (time[i] != '0')
     91             break;//找到time数组中第一个不为的位置
     92         for (j = i; j < lentime; j++)
     93             a[k++] = time[j];
     94         if (i == lentime)
     95             a[k++] = '0';
     96         a[k] = '';
     97         strcpy(arr, temp);
     98     }
     99 }
    100 int main()
    101 {
    102     char a[1005], b[1005], out[1005], arr[1005];
    103     while (scanf("%s%s", a, b) != EOF)
    104     {
    105         BigNumDividBig(out, arr, a, b);
    106         printf("%s
    ", arr);
    107     }
    108     return 0;
    109 }
    110 
    111 
    112 
    113 //#include<stdio.h>
    114 //#include<string.h>
    115 //void BigSubBig(char *a, char *b, char *c)
    116 //{
    117 //    //a表示结果,b被减数,c减数,不能得到负数,需要进入函数之前判断
    118 //    int a_int[1005] = { 0 }, b_int[1005] = { 0 }, c_int[1005] = { 0 };
    119 //    int len1, len2, len, i, owe;
    120 //    len1 = strlen(b);
    121 //    len2 = strlen(c);
    122 //    for (i = 0; i < len1; i++)
    123 //        b_int[i] = b[len1 - 1 - i] - '0';
    124 //    for (i = 0; i<len2; i++)
    125 //        c_int[i] = c[len2 - 1 - i] - '0';
    126 //    len = len1>len2 ? len1 : len2;
    127 //    owe = 0;
    128 //    for (i = 0; i < len; i++)
    129 //    {
    130 //        a_int[i] = b_int[i] - c_int[i] - owe;
    131 //        owe = 0;
    132 //        if (a_int[i] < 0)
    133 //        {
    134 //            owe = 1;
    135 //            a_int[i] = a_int[i] + 10;
    136 //        }
    137 //    }
    138 //    while (a_int[len - 1] == 0 && len != 1)
    139 //        len--;
    140 //    for (i = 0; i<len; i++)
    141 //        a[i] = a_int[len - 1 - i] + '0';
    142 //    a[i] = '';
    143 //}
    144 //int compare(char *a, char *b)
    145 //{
    146 //    int len1 = strlen(a);
    147 //    int len2 = strlen(b);
    148 //    if (len1>len2)
    149 //        return 1;
    150 //    if (len1 < len2)
    151 //        return -1;
    152 //    return strcmp(a, b);
    153 //}
    154 //void BigNumDividBig(char *a, char *arr, char *b, char *c)
    155 //{
    156 //
    157 //    char temp[1005], time[1005];
    158 //    int j, lentime, len, i, flag, k;
    159 //    if (compare(b, c) == -1)
    160 //    {
    161 //        a[0] = '0';
    162 //        a[1] = '';
    163 //        strcpy(arr, b);
    164 //    }
    165 //    else if (compare(b, c) == 0)
    166 //    {
    167 //        a[0] = '1';
    168 //        a[1] = '';
    169 //        arr[0] = '0';
    170 //        arr[1] = '';
    171 //    }
    172 //    else
    173 //    {
    174 //        j = lentime = 0;
    175 //        len = strlen(b);
    176 //        memset(temp, 0, sizeof(temp));
    177 //        memset(time, 0, sizeof(time));
    178 //        for (i = 0; i < len; i++)
    179 //        {//计算得到被除数的前几位,得到整型数组形式的商
    180 //            //time的一个元素表示一次相除的商
    181 //            temp[j++] = b[i];
    182 //            flag = 0;
    183 //            while (compare(temp, c) >= 0)
    184 //            {
    185 //                BigSubBig(temp, temp, c);
    186 //                time[lentime]++;
    187 //                flag = 1;//控制time的元素的位置
    188 //            }
    189 //            if (flag)//将商转换为字符
    190 //                time[lentime] += '0';
    191 //            else//当被除数前几位小于除数,商补    
    192 //                time[lentime] = '0';
    193 //
    194 //            if (!strcmp(temp, "0"))//若temp为''    
    195 //                j = 0;
    196 //            else//继续在b的后面加值    
    197 //                j = strlen(temp);
    198 //            lentime++;
    199 //        }
    200 //        k = 0;
    201 //        for (i = 0; i < lentime; i++)
    202 //        if (time[i] != '0')
    203 //            break;//找到time数组中第一个不为的位置
    204 //        for (j = i; j < lentime; j++)
    205 //            a[k++] = time[j];
    206 //        if (i == lentime)
    207 //            a[k++] = '0';
    208 //        a[k] = '';
    209 //        strcpy(arr, temp);
    210 //    }
    211 //}
    212 //int main()
    213 //{
    214 //    char a[1005], b[1005], out[1005], arr[1005];
    215 //    while (scanf("%s%s", a, b) != EOF)
    216 //    {
    217 //        BigNumDividBig(out, arr, a, b);
    218 //        printf("%s
    ", arr);
    219 //    }
    220 //    return 0;
    221 //}
    View Code

     高精度阶乘

     1 #include<stdio.h>
     2 #include<string.h>
     3 char s[50000];
     4 int a_int[50000] = { 0 }, b_int[50000] = { 0 };
     5 void BigNumMultiSmall(char *a, char *b, int mul)
     6 {
     7     //a表示结果,b表示被乘数,mul表示乘数
     8     int i, j, len;
     9     memset(a_int, 0, sizeof(a_int));
    10     memset(b_int, 0, sizeof(b_int));
    11     len = strlen(b);
    12     for (i = 0; i < len; i++)
    13         b_int[i] = b[len - 1 - i] - '0';
    14     for (i = 0; i<len; i++)
    15     {
    16         a_int[i] = a_int[i] + b_int[i] * mul;
    17         if (a_int[i]>9)
    18         {
    19             a_int[i + 1] = a_int[i] / 10;
    20             a_int[i] = a_int[i] % 10;
    21         }
    22     }
    23     while (a_int[i])
    24     {
    25         a_int[i + 1] = a_int[i] / 10;
    26         a_int[i] = a_int[i] % 10;
    27         i++;
    28     }
    29     while (a_int[i - 1] == 0)
    30         i--;
    31     for (j = 0; j < i; j++)
    32         a[j] = a_int[i - j - 1] + '0';
    33     a[j] = '';
    34 }
    35 int main()
    36 {
    37     int n, i;
    38     while (scanf("%d", &n) != EOF)
    39     {
    40         s[0] = '1';
    41         s[1] = '';
    42         for (i = 1; i <= n; i++)
    43             BigNumMultiSmall(s, s, i);
    44         int len = strlen(s);
    45         printf("%s
    ", s);
    46     }
    47     return 0;
    48 }
    View Code

    高精度小数乘方

      1 #include<stdio.h>
      2 #include<string.h>
      3 char out[20000];
      4 void BigMultiBig(char *a, char *b, char *c)
      5 {
      6     int i, j, len1, len2, len;
      7     int a_int[20010] = { 0 }, b_int[10000] = { 0 }, c_int[10000] = { 0 };
      8 
      9     len1 = strlen(b);
     10     for (i = len1 - 1; i >= 0; i--)
     11         b_int[len1 - i - 1] = b[i] - '0';
     12     len2 = strlen(c);
     13     for (i = len2 - 1; i >= 0; i--)
     14         c_int[len2 - i - 1] = c[i] - '0';
     15 
     16     len = len1 + len2;
     17 
     18     for (i = 0; i < len1; i++)
     19     for (j = 0; j < len2; j++)
     20         a_int[i + j] += b_int[i] * c_int[j];
     21     for (i = 0; i<len; i++)
     22     if (a_int[i]>9)
     23     {
     24         a_int[i + 1] += a_int[i] / 10;
     25         a_int[i] = a_int[i] % 10;
     26     }
     27     while (a_int[len - 1] == 0)
     28         len--;
     29 
     30     for (i = 0; i < len; i++)
     31         a[i] = a_int[len - i - 1] + '0';
     32     a[i] = '';
     33     if (strlen(a) == 0)
     34         strcpy(a, "0");
     35 }
     36 int judge0(int a)
     37 {
     38     int i;
     39     int len = strlen(out);
     40     for (i = a; i < len; i++)
     41     if (out[i] != '0')
     42         return 1;
     43     return 0;
     44 }
     45 int main()
     46 {
     47     char ch[1000];
     48     int n, i, point, j;
     49     while (scanf("%s%d", ch, &n) != EOF)
     50     {
     51         strcpy(out, "1");
     52         int len = strlen(ch);
     53         point = 0;
     54         for (i = 0; i < len; i++)
     55         if (ch[i] == '.')
     56         {
     57             point = (len - i - 1)*n;
     58             for (j = i + 1; j <= len; j++)
     59                 ch[j - 1] = ch[j];
     60         }
     61         for (i = 0; i<n; i++)
     62             BigMultiBig(out, out, ch);
     63         if (strcmp(out, "0") == 0)
     64         {
     65             printf("0
    ");
     66             continue;
     67         }
     68 
     69         int l = strlen(out);
     70         if (l>point)
     71         {
     72             for (i = 0; i < l; i++)
     73             {
     74                 if (judge0(i) == 0 && i >= (l - point))
     75                 {
     76                     if (out[i] != '0')
     77                         printf("%c", out[i]);
     78                     break;
     79                 }
     80                 if (i == l - point)
     81                     printf(".");
     82                 printf("%c", out[i]);
     83             }
     84             printf("
    ");
     85         }
     86         else
     87         {
     88             int flag = 1;
     89             for (i = 0; i < point - l + 1; i++)
     90             {
     91                 if (flag)
     92                 {
     93                     flag = 0;
     94                     printf(".");
     95                 }
     96                 else printf("0");
     97             }
     98             for (i = 0; i < l; i++)
     99             {
    100                 if (judge0(i) == 0)
    101                     break;
    102                 printf("%c", out[i]);
    103             }
    104             printf("
    ");
    105         }
    106     }
    107     return 0;
    108 }
    View Code

    高精度小数加法

      1 #include<stdio.h>
      2 #include<string.h>
      3 void BigAddBig(char *a, char *b, char *c)
      4 {
      5     //a表示结果,b,c位加数
      6     int a_int[1005] = { 0 }, b_int[1005] = { 0 }, c_int[1005] = { 0 };
      7     int len1, len2, len, i;
      8     len1 = strlen(b);
      9     len2 = strlen(c);
     10     for (i = 0; i < len1; i++)
     11         b_int[i] = b[len1 - 1 - i] - '0';
     12     for (i = 0; i<len2; i++)
     13         c_int[i] = c[len2 - 1 - i] - '0';
     14     len = len1>len2 ? len1 : len2;
     15     for (i = 0; i<len; i++)
     16     {
     17         a_int[i] += b_int[i] + c_int[i];
     18         if (a_int[i]>9)
     19         {
     20             a_int[i + 1] = a_int[i] / 10;
     21             a_int[i] = a_int[i] % 10;
     22         }
     23     }
     24     if (a_int[i] != 0)
     25         len++;
     26     while (!a_int[len - 1])
     27         len--;
     28     for (i = 0; i < len; i++)
     29         a[i] = a_int[len - 1 - i] + '0';
     30     a[i] = '';
     31 }
     32 int faddf(char *a, char *b, char *c)
     33 {
     34     int len1, len2, len, i;
     35     int flag = 0;
     36     int a_int[500] = { 0 }, b_int[500] = { 0 }, c_int[500] = { 0 };
     37     len1 = strlen(b);
     38     len2 = strlen(c);
     39     len = len1 > len2 ? len1 : len2;
     40     for (i = 0; i<len1; i++)
     41         b_int[i] = b[i] - '0';
     42     for (i = 0; i<len2; i++)
     43         c_int[i] = c[i] - '0';
     44     for (i = len - 1; i>0; i--)
     45     {
     46         a_int[i] += b_int[i] + c_int[i];
     47         if (a_int[i]>9)
     48         {
     49             a_int[i - 1]++;
     50             a_int[i] = a_int[i] % 10;
     51         }
     52     }
     53     a_int[0] += b_int[0] + c_int[0];
     54     if (a_int[0] > 9)
     55     {
     56         a_int[0] = a_int[0] % 10;
     57         flag = 1;
     58     }
     59 
     60     while (1)
     61     {
     62         if (a_int[len - 1] == 0)
     63             len--;
     64         else break;
     65     }
     66     for (i = 0; i < len; i++)
     67         a[i] = a_int[i] + '0';
     68     a[i] = '';
     69     return flag;
     70 }
     71 int judge(char *a)
     72 {
     73     int len = strlen(a);
     74     for (int i = 0; i < len; i++)
     75     if (a[i] != '0')
     76         return 0;
     77     return 1;
     78 }
     79 int main()
     80 {
     81     char ch1[500], ch2[500], out_int[500];
     82     char inter1[500], inter2[500], float1[500], float2[500], float0[500];
     83     int i;
     84     while (scanf("%s%s", ch1, ch2) != EOF)
     85     {
     86         int len1 = strlen(ch1);
     87         int len2 = strlen(ch2);
     88         int c = 0;
     89         for (i = 0; i < len1; i++)
     90         {
     91             if (ch1[i] == '.')
     92                 break;
     93             inter1[c++] = ch1[i];
     94         }
     95         inter1[c] = '';
     96         c = 0;
     97         for (i = i + 1; i < len1; i++)
     98         {
     99             float1[c++] = ch1[i];
    100         }
    101         float1[c] = '';
    102         c = 0;
    103         for (i = 0; i < len2; i++)
    104         {
    105             if (ch2[i] == '.')
    106                 break;
    107             inter2[c++] = ch2[i];
    108         }
    109         inter2[c] = '';
    110         c = 0;
    111         for (i = i + 1; i < len2; i++)
    112         {
    113             float2[c++] = ch2[i];
    114         }
    115         float2[c] = '';
    116         BigAddBig(out_int, inter1, inter2);
    117         int flag = faddf(float0, float1, float2);
    118         if (flag)
    119         {
    120             char one[2] = "1";
    121             BigAddBig(out_int, out_int, one);
    122         }
    123         int len = strlen(out_int);
    124         if (len == 0)
    125             strcpy(out_int, "0");
    126         if (judge(float0))
    127             printf("%s
    ", out_int);
    128         else printf("%s.%s
    ", out_int, float0);
    129     }
    130 }
    View Code
  • 相关阅读:
    HDU 5486 Difference of Clustering 图论
    HDU 5481 Desiderium 动态规划
    hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
    HDU 5478 Can you find it 随机化 数学
    HDU 5477 A Sweet Journey 水题
    HDU 5476 Explore Track of Point 数学平几
    HDU 5475 An easy problem 线段树
    ZOJ 3829 Known Notation 贪心
    ZOJ 3827 Information Entropy 水题
    zoj 3823 Excavator Contest 构造
  • 原文地址:https://www.cnblogs.com/zyxStar/p/4580305.html
Copyright © 2011-2022 走看看