• 大数模板


    大数加法

    void jial( )
    {
    memset( sun,
    0,sizeof( sun ) );
    int len1 = strlen( str1 ),len2 = strlen( str2 );
    for( int i = 0; i < ( len1 > len2 ? len1 : len2 ); ++i )//转换成ASCII码
    str1[i] -= '0',str2[i] -= '0';
    for( int p = 0, q = len1 - 1; p < q; ++p , --q ) //逆序
    {
    char c = str1[q];
    str1[q]
    = str1[p];
    str1[p]
    = c;
    }
    for( int p = 0, q = len2 - 1; p < q; ++p,--q)
    {
    char c = str2[q];
    str2[q]
    = str2[p];
    str2[p]
    = c;
    }
    for( int i = 0,c = 0; i < ( len1 > len2 ? len1 : len2 ) || c; ++i )//相加 进位
    {
    if( i < len1 )
    c
    += str1[i];
    if( i < len2 )
    c
    += str2[i];
    sun[i]
    = c % 10;//不同的地方1
    c /= 10;//不同的地方2
    }
    }

    大数乘法

    1 #include "stdio.h"
    2 #define MAX 21
    3
    4 void InputNumber(int []); //输入大数
    5 void InitNumber(int []); //初始化用于存放结果的数组
    6 void Multi(int [],int [],int []); //进行大数乘法
    7 void singleMulti(int [],int,int []); //对大数乘以一位数的函数
    8 void Add(int [],int []); //对结果进行错位相加
    9 void printresult(int []); //输出结果
    10
    11 main()
    12 {
    13 int largenumber1[MAX],largenumber2[MAX],mResult[2 * MAX -1];
    14 //largenumber1[]和largenumber2[]表示要相乘的大数
    15 //mResult[]表示结果存放的数组,显然其位数不会超过2 * (MAX - 1)
    16 printf("Input the first number:\n");
    17 InputNumber(largenumber1);
    18 printf("Input the second number:\n");
    19 InputNumber(largenumber2);
    20 InitNumber(mResult);
    21 Multi(largenumber1,largenumber2,mResult);
    22 printf("The result is:\n");
    23 printresult(mResult);
    24 getchar();
    25 }
    26
    27 void InputNumber(int largenumber[])
    28 {
    29 char numberchar;
    30 int i = 0;
    31 scanf("%c",&numberchar);
    32 while(numberchar != '\n' && i < MAX) //最初输入大数用字符串表示
    33 {
    34 largenumber[++i] = numberchar - '0';
    35 scanf("%c",&numberchar);
    36 }
    37 largenumber[0] = i;
    38 }
    39
    40 void InitNumber(int result[])
    41 {
    42 for(int i = 0; i < 2 * MAX - 1; i++)
    43 result[i] = -1;
    44 }
    45
    46 void Multi(int number1[],int number2[],int result[])
    47 {
    48 int i;
    49 int temper[2 * MAX - 1];
    50 for(i = number2[0]; i >= 1; i--)
    51 {
    52 singleMulti(number1,number2[i],temper);
    53 Add(result,temper);
    54 }
    55 }
    56
    57 void singleMulti(int number1[],int number2,int temper[])
    58 {
    59 int i,t = 0,c = 2 * MAX - 2;
    60 for(i = number1[0]; i >= 1; i--)
    61 {
    62 int tempernumber = number2 * number1[i];
    63 temper[c--] = (tempernumber + t) % 10;
    64 t = (int)((tempernumber + t) / 10);
    65 }
    66 if (t > 0)
    67 {
    68 temper[c] = t;
    69 temper[0] = 2 * MAX - 2 - c + 1;
    70 }
    71 else
    72 temper[0] = 2 * MAX -2 - c;
    73 }
    74
    75 void Add(int result[],int temper[])
    76 {
    77 static int pos = 2 * MAX - 2;
    78 int c = pos,t = 0;
    79 for(int i = 2 * MAX - 2; i > 2 * MAX - 2 - temper[0]; i--)
    80 {
    81 if (result[c] == -1)
    82 result[c] = 0;
    83 int tempernumber = result[c] + temper[i];
    84 result[c--] = (tempernumber + t) % 10;
    85 t = (int)(tempernumber / 10);
    86 }
    87 if (t == 1)
    88 result[c] = 1;
    89 pos--;
    90 }
    91
    92 void printresult(int result[])
    93 {
    94 for(int i = 1; i < 2 * MAX - 1 ; i++)
    95 if (result[i] != -1)
    96 printf("%d",result[i]);
    97 }

  • 相关阅读:
    处理MySQL的ibdata1文件过大问题
    关于mysql启动问题---mysqld_safe mysqld from pid file * ended
    mysql数据库指定ip远程访问
    关于access_log 日志文件以及ip、uv和pv的定义
    nginx配置及内核优化详解
    LN : leetcode 746 Min Cost Climbing Stairs
    LN : leetcode 684 Redundant Connection
    LN : leetcode 730 Count Different Palindromic Subsequences
    LN : leetcode 516 Longest Palindromic Subsequence
    LN : leetcode 215 Kth Largest Element in an Array
  • 原文地址:https://www.cnblogs.com/jian1573/p/2013910.html
Copyright © 2020-2023  润新知