大数除法 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.
![](http://uva.onlinejudge.org/external/104/p10494.gif)
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;}
- 大数除法 10494 - If We Were a Child Again
- UVA 10494 - If We Were a Child Again(大数除法和取模)
- UVA 10494-If We Were a Child Again(大数除法及取余)
- UVA 10494 If We Were a Child Again(大数)
- (高精度运算4.7.27)UVA 10494If We Were a Child Again(大数除法&&大数取余)
- 10494 If We Were a Child Again
- 10494 - If We Were a Child Again
- 10494 - If We Were a Child Again
- 10494 - If We Were a Child Again
- If We Were a Child Again(大数除法、求余)
- UVA 10494 If We Were a Child Again 高精度除法简单版
- UVA 10494-If We Were a Child Again(大数类)
- uva10494 - If We Were a Child Again
- 5312 If We Were a Child Again
- If We Were a Child Again
- If We Were a Child Again
- uva10494 if we were a child again
- uva If We Were a Child Again
- yum 报错Loading mirror speeds from cached hostfile
- Linux cpuinfo 详解
- 如果矩阵中某个元素为0,那么把它所在的行和列都置为0
- spring
- fcntl设置FD_CLOEXEC标志作用
- 大数除法 10494 - If We Were a Child Again
- Fatal error: Call to undefined function OCILogon() in D:\xampp\htdocs\chaxun\rbora.php on line 38
- simpson paradox
- Linux下查看端口占用进程号,程序名的方法
- ICE第二篇--一个"hello world"的简单例子
- MyEclipse快捷键设置(全面升级版本)
- 【Oracle】-【v$session】v$session的SNIPED状态
- 性能loadrunner--loadrunner测试工程
- 【js代码调试】Console命令详解,让调试js代码变得更简单