我试图按照标题中的建议将数字除以整数,我很难这样做。我正在考虑将大数字的数字插入到矢量中,然后重复从矢量中减去所述数字,直到它为空。但是,我找不到代码来做到这一点。以下是我到目前为止所尝试的内容:
#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;
}
答案 0 :(得分:1)
您可以使用GMP而不是从头开始编写。下载GMP源代码,构建它并学习如何使用它所花费的时间比自己编写它要少。
基础2 ^ 32或2 ^ 64的铅笔和纸张分割比“逐减法”效率更高,我相信GMP采用的算法比那更好。
使用GMP你会写:
mpz_class a("134897563047067890568704560457984059182035823590780968094759123590346040967804956029586405960249562895760983475187459073406984560900123895702851034560016405716045613495619456196450196450165901268051620465016405634056104951923845902387581");
std::cout << a / 7 << std::endl;