马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
传送门:https://pintia.cn/problem-sets/994805260223102976/problems/994805305181847552
解:#include<cstdio>
#include<cstring>
typedef struct BIGN{
int num[1000];
int len;
bool is_neg;
BIGN()
{
memset(num, 0, sizeof(num));
len = 0;
is_neg = false;
};
}bign;
bign str2bign(char *s)
{
bign a;
a.len = strlen(s);
if (s[0] == '-')
{
a.is_neg = true;
a.len--;
s++;
}
for (int i = 0; i < a.len; i++)
a.num[i] = s[a.len - 1 - i] - '0';
return a;
}
void print_bign(bign a)
if (a.is_neg)
putchar('-');
for (int i = a.len - 1; i >= 0; i--)
printf("%d", a.num[i]);
}
bign divi(bign a, int b, int &r)
{
bign c;
if (!b)
{
puts("0 can not be divisor!!");
return c;
}
if (a.is_neg + (b < 0) == 1)
c.is_neg = true;
if (b < 0)
b = -b;
c.len = a.len;
for (int i = a.len - 1; i >= 0; i--)
{
r = r * 10 + a.num[i];
c.num[i] = r / b;
r = r % b;
}
while (c.len > 1 && !c.num[c.len - 1])
c.len--;
return c;
}
int main(void)
{
int r = 0, b;
char s[1010];
scanf("%s %d", s, &b);
bign a = str2bign(s);
print_bign(divi(a, b, r));
printf(" %d", r);
}
|