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

来源:互联网 发布:c语言教程全套 编辑:程序博客网 时间:2024/06/14 00:38

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,0,sizeof(big));        memset(di,0,sizeof(di));        memset(mod,0,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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 有一块牙龈肿了怎么办 耳前瘘管有脓怎么办 睡歪脖子很痛怎么办 睡歪脖子怎么办小妙招 睡觉睡得脖子疼怎么办 睡久了后脑勺疼怎么办 腮帮肿了但不疼怎么办 吃硬东西腮帮痛怎么办 嘴巴长开旁边疼怎么办 晚上腿酸睡不着觉怎么办 吃酸的牙齿酸软怎么办 吃酸东西牙齿酸怎么办 拔完智齿神经疼怎么办 22岁腿关节疼痛怎么办 耳洞里面有硬块怎么办 耳洞发炎有硬块怎么办 小孩慢性扁桃体发炎怎么办大全 侧切伤口化脓了怎么办 狗狗伤口不愈合怎么办 狗后腿烂了化脓怎么办 乳腺增生腋下淋巴结肿大怎么办 手术刀口冒脓了怎么办 脚伤口感染肿了怎么办 皮外伤口不愈合怎么办 烫伤新皮红红的怎么办 脸上痘痘化脓了怎么办 痘痘引起的红肿怎么办 痘痘引起的硬块怎么办 痘痘化脓感染了怎么办 烫伤有脓了烂掉怎么办 烫伤起泡破皮了怎么办 一出汗就有狐臭怎么办 耳朵后面臭臭的怎么办 赤黑abo意外怀孕怎么办 qq忘记密保了怎么办 三星s8耗电太快怎么办 360云盘关闭照片怎么办 360云盘停止服务怎么办 房间湿度90%太高怎么办 天刀掠夺进错了怎么办 狗耳朵臭怎么办黄垢