怎样处理一个大数除法呢?
下面介绍一下处理大数除法的方法:
将大数存入字符数组 s[100] 中 , 定义一个整型数组 a[100] , 将字符数组 s 中的每个字符变成相应的整型数值,并存入整型数组a中;
取出a中的第一位,让它与除数相除,看结果是否为 0 ,若不为 0 ,则直接输出结果,若为0,不输出,记下余数,下一次:res = a[i] + res * 10 ,如此循环下去即可;
下面给出参考代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 |
#include<iostream> #include<string.h> #include<string> #include<stdio.h> using
namespace std ; void
chu( char
s[] , long
long int a ) { long
long int len = strlen (s) ; long
long int rem = 0 , f = 1 , i ; for ( i = 0 ; i < len ; i++) { rem = s[i] - ‘0‘
+ rem * 10 ; if (rem/a != 0) f = 0 ; if (!f) printf ( "%lld" ,rem/a) ; rem = rem % a ; } if (f) cout << "0"
; cout << endl ; } void
yu( char
s[] , long
long int a) { long
long int len = strlen (s) ; long
long int rem = 0 , i ; for (i = 0 ; i < len ; i++) { rem = s[i] - ‘0‘
+ rem * 10 ; rem = rem % a ; } printf ( "%lld\n" ,rem) ; } int
main() { char
s[100000] , c ; long
long int a ; while ( scanf ( "%s %c %lld" ,s,&c,&a) == 3) { if (c == ‘/‘ ) chu(s,a) ; else yu(s,a) ; } return
0 ; } |
原文:http://www.cnblogs.com/scottding/p/3662794.html