大数计算

#include <iostream>

#include <vector>

#include <stdlib.h>

 

void main(void)

{

       // 以下算法计算nm次方

       // m的定义域是[1,2^31]n的定义域是[0,65535]

       // 原理就是按位相乘,处理进位

 

    int n = 26, m = 20;

       div_t dr={0};

    std::vector<int> vecNum(1,n);    /* <int>说明这个vector是用来存放int的。用vector存储大数,首位赋n div_t results = std::div(93, 17);*/

       for(int i = 0; i< m-1; ++i ,dr.quot=0)   // 每次将上次的商清零,循环乘m

    {

              // 对大数的每一位都乘以n

              for (std::vector<int>::iterator cur = vecNum.begin();cur != vecNum.end();*(cur++) = dr.rem)

              { 

                     /*                  vector

                            {

                                   typedef   T*   iterator;

                            }

                      */

                     // 加上上一位的余数

                     *cur = *cur * n + dr.quot;

                     // 除的余,保留个位数 

            dr = div(*cur, 10);   

        }                 // 下面的循环处理进位使位数增加的情况

for (dr = div(dr.quot,10);dr.quot || dr.rem;dr = div(dr.quot,10))

vecNum.push_back(dr.rem);

    }          // 下面输出,从最后的最高位开始

    for (std::vector<int>::reverse_iterator rcur = vecNum.rbegin();rcur != vecNum.rend(); ++rcur)

       {            std::cout << *rcur;

       }

       std::cout<<std::endl;

       std::cin.get();

}

你可能感兴趣的:(算法,vector,iterator,存储,div,include)