UVA10494 If We Were a Child Again

本文介绍了一种使用自定义大数结构处理高精度算术运算的方法,特别关注于整数除法和取模操作的实现。通过分析一个具体的编程实例,展示了如何将字符串形式的大数转换为可操作的数据结构,并提供了高效的除法和取模算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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 }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值