UVA10494 If We Were a Child Again

 big number的收关题,自己的思路过于复杂,所以看了题解,感觉题目出的宽泛,除数可以用long存下来就省了很多麻烦。加减乘用bign完全没得问题,但是高精度的除法一直不知道快捷的算法是什么?

题目:

Problem C: If We Were a Child Again

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

 

 

 

 


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

 

解答:

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 struct bign
 5 {
 6     int len;
 7     int s[1000];
 8 };
 9 bign a,res;
10 char str[1000];
11 long b;
12 void change(bign *a)
13 {
14     int i;
15     a->len=strlen(str);
16     for(i=0;i<a->len;i++)
17         a->s[i]=str[i]-'0';
18 }
19 void div()
20 {
21     int i,j=0;
22     long num=0;
23     for(i=0;i<a.len;i++)
24     {
25         num=num*10+a.s[i];
26         res.s[j]=num/b;
27         num=num%b;
28         j++;
29     }
30     (&res)->len=j;
31 }
32 long mod()
33 {
34     int i,j;
35     long ans=0;
36     for(i=0;i<a.len;i++)
37     {
38         ans=ans*10+a.s[i];
39         ans=ans%b;
40     }
41     return ans;
42 }
43 int main()
44 {
45     char c;
46     while(scanf("%s %c %ld",str,&c,&b)!=EOF)
47     {
48         change(&a);
49         if(c=='/')
50         {
51             int i=0,j;
52             div();
53             while(i<res.len-1&&res.s[i]==0)
54                 ++i;
55             for(j=i;j<res.len;j++)
56             {
57                 printf("%d",res.s[j]);
58             }
59             printf("\n");
60         }
61         else if(c=='%')
62         {
63             long aa;
64             aa=mod();
65             printf("%ld\n",aa);
66         }
67     }
68     return 0;
69 }

 

 

 

posted @ 2013-02-07 19:08  上白泽慧音  阅读(615)  评论(0编辑  收藏  举报