UVA_If We Were a Child Again

Description

 

If We Were a Child Again


“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.





Problemsetter:  S. M. Ashraful Kadir, University of Dhaka

 

 
题意:大数除小数;大数模小数问题;
这里0 < n < 231 ;
n用long long就行了;
 
注意:模板里面一起同步将int 改为 long long ;
 
代码:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #define UNIT 10
 6 
 7 using namespace std;
 8 
 9 struct Bignum
10 {
11     long long val[10000];
12     int len;
13 
14     Bignum ()
15     {
16         memset (val, 0, sizeof(val));
17         len = 1;
18     }
19 
20     Bignum operator / (const long long &a) const//大数除小数
21     {
22         Bignum x;
23         long long down = 0;
24         for (int i = len-1; i >= 0; --i)
25         {
26             x.val[i] = (val[i]+down*UNIT) / a;
27             down = val[i] + down*UNIT - x.val[i]*a;
28         }
29         x.len = len;
30         while (x.val[x.len-1] == 0 && x.len > 1)
31             x.len--;
32         return x;
33     }
34 
35     long long operator % (const long long &a) const//大数模小数
36     {
37         int x = 0;
38         for (int i = len-1; i >= 0; --i)
39             x = ((x*UNIT)%a+val[i]) % a;
40         return x;
41     }
42 
43 
44 };
45 
46 
47 char a[10000],ch;
48 long long b;
49 int main()
50 {
51     //freopen("ACM.txt","r",stdin);
52 
53     while(scanf("%s %c %lld", a, &ch, &b)!=EOF)
54     {
55        // printf("%c",ch);
56         Bignum x1,ans;
57         int La=strlen(a);
58         x1.len=0;
59         for(int i=La-1;i>=0;i--)
60         x1.val[x1.len++]=a[i]-'0';
61        // cout<<ch<<b<<endl;
62         if(ch=='/')
63         {
64             ans=x1/b;
65             for(int i=ans.len-1;i>=0;i--)
66             cout<<ans.val[i];
67             cout<<endl;
68         }
69         else if(ch=='%')
70         {
71             cout<<x1%b<<endl;
72         }
73     }
74     return 0;
75 }
View Code

 

posted on 2014-10-25 15:24  M.D.LUFFI  阅读(163)  评论(0编辑  收藏  举报

导航