(若想查看高精除以单精,请点击链接)链接 嗯,这个还有点儿难度(虽然不常用),其实也不难,稍微讲讲吧!
在用竖式计算除法的时候,用减法模拟每次的相除,从高位到低位,每次减到不能再减为止,然后向后移一位。嗯,就这样。
Code
#include<bits/stdc++.h>
using namespace std
;
int c
[300];
char ch1
[300],ch2
[300],ans
[300];
bool cmp(int p
,char ch1
[],char ch2
[])
{
int l
=1;
while(ch1
[l
]=='0'&&l
<=p
-1) l
++;
char tch
[300];
int cnt
=0;
for(int i
=l
;i
<=p
;i
++)
{
tch
[++cnt
]=ch1
[i
];
}
tch
[cnt
+1]='\0';
if(strlen(tch
+1)>strlen(ch2
+1)||(strlen(tch
+1)==strlen(ch2
+1)&&strcmp(tch
+1,ch2
+1)>=0)) return true;
else return false;
}
void subtraction(int p
,char ch1
[],char ch2
[])
{
int l
=1;
while(ch1
[l
]=='0'&&l
<=p
-1) l
++;
char tch
[300];
int cnt
=-1;
for(int i
=l
;i
<=p
;i
++)
{
tch
[++cnt
]=ch1
[i
];
}
int ta
[300],tb
[300];
memset(ta
,0,sizeof(ta
));
memset(tb
,0,sizeof(tb
));
int len2
=strlen(ch2
+1);
for(int i
=1;i
<=cnt
+1;i
++) ta
[i
]=tch
[cnt
+1-i
]-'0';
for(int i
=1;i
<=len2
;i
++) tb
[i
]=ch2
[len2
+1-i
]-'0';
int tc
[300];
memset(tc
,0,sizeof(tc
));
for(int i
=1;i
<=cnt
+1;i
++)
{
if(ta
[i
]-tb
[i
]<0)
{
ta
[i
+1]--;
ta
[i
]+=10;
}
tc
[i
]=ta
[i
]-tb
[i
];
}
for(int i
=1;i
<=cnt
+1;i
++)
{
ch1
[l
-1+i
]=tc
[cnt
+2-i
]+'0';
}
}
void add_ans(int p
)
{
ans
[p
]++;
}
int main()
{
cin
>>ch1
+1>>ch2
+1;
int len1
=strlen(ch1
+1);
int len2
=strlen(ch2
+1);
for(int i
=len2
;i
<=len1
;i
++)
{
while(cmp(i
,ch1
,ch2
)==true)
{
subtraction(i
,ch1
,ch2
);
add_ans(i
);
}
}
int l1
=1,l2
=1;
while(ans
[l1
]==0&&l1
<=len1
-1) l1
++;
for(int i
=l1
;i
<=len1
;i
++) printf("%d",ans
[i
]);
cout
<<endl
;
while(ch1
[l2
]=='0'&&l2
<=len1
-1) l2
++;
for(int i
=l2
;i
<=len1
;i
++) printf("%c",ch1
[i
]);
return 0;
}
嗯,还是比较有难度,结合竖式相除的过程再理解一下,应该不会有太大问题。(记住:用高精度减法模拟每位相除的过程)
学习厌倦了?点我有更多精彩哦!