鱼C论坛

 找回密码
 立即注册
查看: 2535|回复: 0

[技术交流] PTA B_1017 A除以B

[复制链接]
发表于 2020-2-1 10:37:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
传送门:https://pintia.cn/problem-sets/994805260223102976/problems/994805305181847552

解:
  1. #include<cstdio>
  2. #include<cstring>

  3. typedef struct BIGN{
  4.     int num[1000];
  5.     int len;
  6.     bool is_neg;
  7.    
  8.     BIGN()
  9.     {
  10.         memset(num, 0, sizeof(num));
  11.         len = 0;
  12.         is_neg = false;
  13.     };
  14. }bign;

  15. bign str2bign(char *s)
  16. {
  17.     bign a;
  18.    
  19.     a.len = strlen(s);
  20.    
  21.     if (s[0] == '-')
  22.     {
  23.         a.is_neg = true;
  24.         a.len--;
  25.         s++;
  26.     }
  27.    
  28.     for (int i = 0; i < a.len; i++)
  29.         a.num[i] = s[a.len - 1 - i] - '0';
  30.         
  31.     return a;
  32. }

  33. void print_bign(bign a)
  34.     if (a.is_neg)
  35.         putchar('-');
  36.         
  37.     for (int i = a.len - 1; i >= 0; i--)
  38.         printf("%d", a.num[i]);
  39. }

  40. bign divi(bign a, int b, int &r)
  41. {
  42.     bign c;
  43.    
  44.     if (!b)
  45.     {
  46.         puts("0 can not be divisor!!");
  47.         
  48.         return c;
  49.     }
  50.    
  51.     if (a.is_neg + (b < 0) == 1)
  52.         c.is_neg = true;
  53.         
  54.     if (b < 0)
  55.         b = -b;
  56.    
  57.     c.len = a.len;   
  58.     for (int i = a.len - 1; i >= 0; i--)
  59.     {
  60.         r = r * 10 + a.num[i];
  61.         c.num[i] = r / b;
  62.         r = r % b;
  63.     }
  64.    
  65.     while (c.len > 1 && !c.num[c.len - 1])
  66.         c.len--;

  67.     return c;
  68. }

  69. int main(void)
  70. {
  71.     int r = 0, b;
  72.     char s[1010];

  73.     scanf("%s %d", s, &b);

  74.     bign a = str2bign(s);

  75.     print_bign(divi(a, b, r));

  76.     printf(" %d", r);
  77. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 11:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表