加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数除法 10494 - If We Were a Child Again

发布时间:2020-12-14 04:05:58 所属栏目:大数据 来源:网络整理
导读:Problem C If We Were a Child Again Input: ?standard input Output: ?standard output Time Limit:? 7?seconds ? “Oooooooooooooooh! If I could do the easy mathematics like my school days!! I can guarantee,that I’d not make any mistake this ti

Problem C
If We Were a Child Again

Input:?standard input
Output:?standard output

Time Limit:?7?seconds

?

“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

?


第一次做大数除法,其实和大数乘法很类似,模拟草稿纸上的运算。

有几个细节需要注意一下,第一个是在除数变化的过程中,可能会超过int型,所以用了double转换了一下

而且数组开得大一些,不然会RE

还有就是要注意到除数为0的特殊情况。一开始没有注意到,wa了几次。。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<ctype.h>
#include<cmath>
#include<algorithm>
char big[1000000];
char str[1000000],ch;
int di[1000000],mod[1000000];
int num;
using namespace std;
int main ()
{
    int i,j,len;
    while(gets(str)!=NULL)
    {
        memset(big,sizeof(big));
        memset(di,sizeof(di));
        memset(mod,sizeof(mod));
        num=0;
        len=strlen(str);
        for (i=0; ; i++)
            if (str[i]!=' ') big[i]=str[i];
            else break;
        for (j=i; ; j++)
            if (str[j]!=' ')
            {
                ch=str[j];
                break;
            }
        j++;
        while(str[j]==' ') j++;
        for (i=j; i<len; i++)
            num=num*10+str[i]-'0';
        for (i=0; i<strlen(big); i++)
        {
            double g,t;
            t=mod[i];
            g=t*10+big[i]-'0';
            di[i]=(int)(g/num);
            t=(int)(g/num);
            mod[i+1]=g-t*num;
        }
        if (ch=='%') cout<<mod[i]<<endl;
        else
        {
            for (i=0; i<strlen(big); i++)
                if (di[i]!=0) break;
            if (i==strlen(big)) cout<<"0";
            else
                for (j=i; j<strlen(big); j++)
                    cout<<di[j];
            cout<<endl;
        }


    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读