zoukankan      html  css  js  c++  java
  • PTA 乙级 1017 A除以B (20 分) C/C++

    1017 A除以B (20 分)

    本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数。你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立。

    输入格式:

    输入在一行中依次给出 A 和 B,中间以 1 空格分隔。

    输出格式:

    在一行中依次输出 Q 和 R,中间以 1 空格分隔。

    输入样例:

    123456789050987654321 7
    

    输出样例:

    17636684150141093474 3

     1 #include<stdio.h>
     2 #include<string.h>
     3 struct bign {
     4     int d[1010];
     5     int len;
     6     bign() {
     7         memset(d, 0, sizeof(d));
     8         len = 0;
     9     }
    10 };
    11 bign change(char str[]) { // 将整数转换为bign
    12     bign a;
    13     a.len = strlen(str);
    14     for(int i = 0; i < a.len; i++) {
    15         a.d[i] = str[i] - '0'; // 高位放进a的低位
    16     }
    17     return a;
    18 }
    19 
    20 void divide(bign a, int b, int& r) { //高精度除法,r为余数
    21     bign c;
    22     c.len = a.len;
    23     for(int i = 0; i < a.len; i++) { //从高位开始
    24         r = r * 10 + a.d[i]; //和上一位遗留的余数组合
    25         if( r < b ) c.d[i] = 0; //不够除,该位为0
    26         else { //够除
    27             c.d[i] = r / b; //
    28             r = r % b; //获得新的余数
    29         }
    30     }
    31     int j = 0;
    32     for(int i = 0; i < c.len - 1 && c.d[i] == 0; i++) {
    33         j++;
    34     }
    35 
    36     // 遍历输出
    37     for(int i = j; i < c.len; i++)
    38         printf("%d", c.d[i]);
    39     printf(" %d", r);
    40 }
    41 
    42 int main() {
    43     char str[1010];
    44     int b = 0, r = 0;
    45     scanf("%s%d", str, &b);
    46     bign a = change(str); //将a转换为bign型
    47     divide(a, b, r);
    48     return 0;
    49 }
     
  • 相关阅读:
    JavaWeb--HttpSession案例
    codeforces B. Balls Game 解题报告
    hdu 1711 Number Sequence 解题报告
    codeforces B. Online Meeting 解题报告
    ZOJ 3706 Break Standard Weight 解题报告
    codeforces C. Magic Formulas 解题报告
    codeforces B. Sereja and Mirroring 解题报告
    zoj 1109 Language of FatMouse 解题报告
    hdu 1361.Parencodings 解题报告
    hdu 1004 Let the Balloon Rise 解题报告
  • 原文地址:https://www.cnblogs.com/dreamcoding/p/10339768.html
Copyright © 2011-2022 走看看