• 大数(string 之间的快速幂)


     1 //字符串的乘法
     2 string multi(string a, string b){
     3     int i, j, arr[200], len = a.length() + b.length();
     4     memset(arr, 0, sizeof arr);
     5     reverse(a.begin(), a.end());
     6     reverse(b.begin(), b.end());
     7     for (int i = 0; i<a.length(); i++)
     8     for (int j = 0; j<b.length(); j++)
     9         arr[i + j] += (a[i] - '0')*(b[j] - '0');
    10     for (int i = 0; i<len; i++){
    11         arr[i + 1] += arr[i] / 10;
    12         arr[i] %= 10;
    13     }
    14     string ret = string(len, '0');
    15     for (int i = 0; i<len; i++)
    16         ret[i] += arr[i];
    17     reverse(ret.begin(), ret.end());//翻转
    18     return ret;
    19 }
    20 
    21 //x ^ n
    22 string Str_Pow(string x, int p){
    23     string ret = "1";
    24     while (p){
    25         if (p & 1) ret = multi(ret, x);
    26         x = multi(x, x);
    27         p >>= 1;
    28     }
    29     return ret;
    30 }
  • 相关阅读:
    阅读ajax.pdf
    XPath 学习
    随想
    Angular JS 学习
    Angular JS 学习
    URL Rewrite in IIS with Regular Pattern
    LinQ In Action 学习第四章
    GC垃圾回收
    jvm底层
    类加载流程
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/8732471.html
Copyright © 2020-2023  润新知