zoukankan      html  css  js  c++  java
  • 大数除法

     

    “Oooooooooooooooh!

    If I could do the easy mathematics like my school days!!

    I can guarantee, that I’d not make any mistake this time!!”

    Says a smart university student!!

    But his teacher even smarter – “Ok! I’d assign you such projects in your software lab. Don’t be so sad.”

    “Really!!” - the students feels happy. And he feels so happy that he cannot see the smile in his teacher’s face.

    The Problem

    The first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.

    But like many other university students he doesn’t like to do any project by himself. He just wants to collect programs from here and there. As you are a friend of him, he asks you to write the program. But, you are also intelligent enough to tackle this kind of people. You agreed to write only the (integer) division and mod (% in C/C++) operations for him.

    Input

    Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign (division or mod). Again spaces. And another input number. Both the input numbers are non-negative integer. The first one may be arbitrarily long. The second number n will be in the range (0 < n < 231).

    Output
    A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.
    Sample Input

    110 / 100

    99 % 10

    2147483647 / 2147483647

    2147483646 % 2147483647

    Sample Output

    1

    9

    1

    2147483646

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <string>
    #include <cstring>
    using namespace std;
    const int N = 10000 ;
    
    int jian(int *a , int len1  , int *b , int len2)
    {
        //判断a是否大于b , 如果否就返回-1 , 否则继续下去
        if(len1 < len2) 
            return -1 ;
        else if(len1 == len2)
        {
            for(int i = len1 - 1 ; i >= 0 ; i--)
            {
                if(a[i] > b[i])
                    break ;
                else if(a[i] == b[i])
                    continue ;
                else
                    return -1 ;
            }
        }
        for(int i = 0 ; i < len2 ; i++) // 注意是len2不是len1网上码错了
        //因为只需减到相同部分长度就可以 , 如果len1 长出来的部分会减去没有赋值的部分,出错!!!
        {
            a[i] -= b[i] ; // 相减
            if(a[i] < 0)
            {
                a[i+1]--;//借位
                a[i] += 10 ;
            }
        }
        for(int i = len1 - 1 ; i >= 0 ; i--)
        {
    
            if(a[i]) // 根据前置零的位置判断被减数的长度
            {
    
                return i + 1 ; // 被减数长度
            }
        }
        return 0 ;//如果被减数减到全为0时返回0 ;
    
    }
    
    
    void multijian(char *a , char *b , char cc)//注意是a / b .
    {
        int c[N] , d[N]  , e[N] = {0} , f[N] = {0};//e作为记录结果商的数组,f记录将除数移位后的数组
        int len1 = strlen(a) , len2 = strlen(b) ;
        int k = 0 , temp = 0 , len = 0 ; // len 商数组的长度 , temp记录每一次减完后的长度
        bool flag = false ; // 主要是标记第一次
        for(int i = len1 - 1 ; i >= 0 ; i--)
            c[k++] = a[i] - '0';
        k = 0 ;
        for(int i = len2 - 1 ; i >= 0 ; i--)
            d[k++] = b[i] - '0' ;
        int times = len1 - len2 ;
        for(int i = times ; i >= 0 ; i--) // i 表示两个数之间的长度差
        {
            for(int j = len2 - 1 ; j >= 0 ; j--)
            {
                f[j + i] = d[j]; // 将除数右移对齐被除数
            }
            for(int j = 0 ; j < i ; j++)
                f[j] = 0 ; //将除数前面的0补全
            while((temp = jian(c , len1 , f , i + len2)) >= 0)//进行减法运算
            {
                e[i]++ ; // 每循环一次商就自加一。
                len1 = temp ; // 更新被除数的长度
            }
            if(e[i] && flag == false)//根据第一个不为0的商的结果来确定商的长度
            {
                len = i + 1 ;
                flag = true ;
            }
        }
         if(cc == '/')
         {
             if(!len) cout << 0 <<endl ;
             else{
             for(int i = len - 1 ; i >= 0 ; i--)
                cout << e[i] ;
            cout << endl ;
             }
         }
         else
         {
             if(!len1) cout << 0 << endl ;
             else{
             for(int i = len1 - 1 ; i >= 0 ; i--)
                cout << c[i];
             cout << endl ;
             }
         }
    }
    
    
    int main()
    {
        char a[N] , b , c[N] ;
        while(~scanf("%s" , &a )) // 连一块输入会出问题
        {
            cin >> b ;
            scanf("%s" , &c) ;
            multijian(a , c , b);
        }
    
        return 0;
    }
  • 相关阅读:
    浅谈C++ STL中的优先队列(priority_queue)
    哈夫曼树与哈夫曼编码
    Binary Search 的递归与迭代实现及STL中的搜索相关内容
    Prime 算法的简述
    估算网站需要多少宽带方法
    微服务服务拆分步骤
    第一次有人把科创板讲得这么简单明了
    一个人优秀到可怕的三个迹象!一旦具备,必为人中龙凤,大富大贵
    美元加息怎么“剪羊毛”
    英国脱欧的影响
  • 原文地址:https://www.cnblogs.com/nonames/p/11203691.html
Copyright © 2011-2022 走看看