算法理解
按原数字从高位到低位模拟竖式。remainder 保存此前余数拼上当前数位后的值,当前商位是 remainder / divisor,之后更新余数。
- 复杂度:
O(n)。 - 注意:除数不能为零;该模板的除数是普通整数,并且不输出余数。
模板代码
#include <bits/stdc++.h>
#include <iterator>
using namespace std;
#define int long long
signed main()
{
string a;
int b;
cin>>a>>b;
int t=0,count=0;
string c="";
if (a=="0")
{
cout<<'0';
return 0;
}
for (int i=0;i<a.size();i++)
{
t=t*10+a[i]-'0';
c+=t/b+'0',t%=b;
}
reverse(c.begin(),c.end());
while (c.back()=='0')
{
c.pop_back();
}
reverse(c.begin(),c.end());
cout<<c;
return 0;
}
