C++ 大数(大于long)和单个整数之间的除法

C++ 大数(大于long)和单个整数之间的除法,c++,division,largenumber,C++,Division,Largenumber,我正试图将一个数字除以标题中建议的整数,但我很难做到这一点。我在考虑将大数字的数字插入向量,然后重复从向量中减去所述数字,直到它为空。但是,我找不到执行此操作的代码。以下是我迄今为止一直在尝试的: #include <fstream> using namespace std; int v[100],r[100]; //In v i store the initial number,in r i store the result int main() { FILE*fin=fo

我正试图将一个数字除以标题中建议的整数,但我很难做到这一点。我在考虑将大数字的数字插入向量,然后重复从向量中减去所述数字,直到它为空。但是,我找不到执行此操作的代码。以下是我迄今为止一直在尝试的:

   #include <fstream>
using namespace std;
int v[100],r[100];  //In v i store the initial number,in r i store the result
int main()
{
FILE*fin=fopen("imp.in","r");
FILE*fout=fopen("imp.out","w");
int n,x,c,w,k,m=2;
fscanf(fin,"%d",&n); //Reads the number of digits the number has
for(;n>0;--n)
{
    fscanf(fin,"%d",&x);
    v[n]=x;
}
fscanf(fin,"%d",&c); //Reads the digit it has to be divided by
while(v[n]!=0)
{
   k=0;
   while(v[1]>=c)
   {
        v[1]-=c;
        ++k;
   }//As long as the first position in vector can be substracted, increase k
   --v[2];
   v[1]+=10;
    /* Because first position will be negative, take one from second                    position and add ten in the first*/
   w=2;
   while(v[w]<0)
   {
       v[w]=9;
       --v[w+1];
       ++w;
   }
   /*When, for example, you substract one from 1000,it will become 999. This loop helps do that*/
   r[1]+=k;
   if(r[1]>9)
   {
       r[1]-=10;
       w=2;
       ++r[2];
       while(r[w]>9)
       {
           ++r[w+1];
           r[w]=0;
           ++w;
           if(w>m)m=w;
       }
   }
   /*If statement and the line of code above it inserts the result into r[100]*/
}
for(;w>0;--w)fprintf(fout,"%d",r[w]);
return 0;
}
#包括
使用名称空间std;
int v[100],r[100]//在v中我存储初始数字,在r中我存储结果
int main()
{
文件*fin=fopen(“imp.in”,“r”);
文件*fout=fopen(“imp.out”,“w”);
int n,x,c,w,k,m=2;
fscanf(fin,'%d',&n);//读取数字的位数
对于(;n>0;--n)
{
fscanf(fin、%d、&x);
v[n]=x;
}
fscanf(fin、%d、&c);//读取它必须除以的数字
而(v[n]!=0)
{
k=0;
而(v[1]>=c)
{
v[1]=c;
++k;
}//只要向量中的第一个位置可以减去,就增加k
--v[2];
v[1]+=10;
/*因为第一个位置是负数,所以从第二个位置取一,在第一个位置加十*/
w=2;
while(v[w]9)
{
r[1]=10;
w=2;
++r[2];
而(r[w]>9)
{
++r[w+1];
r[w]=0;
++w;
如果(w>m)m=w;
}
}
/*If语句及其上方的代码行将结果插入r[100]*/
}
对于(;w>0;--w)fprintf(fout,“%d”,r[w]);
返回0;
}
您可以使用它,而不是自己从头开始编写。与自己编写相比,下载GMP源代码、构建和学习如何使用它所花费的时间更少

以2^32或2^64为基数的纸笔除法将比“减法除法”更有效,我相信GMP采用的算法比这更好

有了GMP,你会写:

mpz_class a("134897563047067890568704560457984059182035823590780968094759123590346040967804956029586405960249562895760983475187459073406984560900123895702851034560016405716045613495619456196450196450165901268051620465016405634056104951923845902387581");
std::cout << a / 7 << std::endl;
mpz_a类(“13489756304706789056870456045798405918203582359078096809475912359034640967804560295864059602495628957609834751874590734069845609001238957085102851034560016405716045613495619456450196450165901268051620464056340549519238902387581”);

我强烈建议使用一个大数字库来完成这项工作,而不是编写自己的实现。搜索谷歌“128位师C++”,不要使用BooS.Mulk精度。我需要最多2000位的数字。我知道在代码中我只使用了100位数字,但我可以随时更改。因此,我需要自己编写实现。