1、大整数相加
1: static void plus(String input1, String input2) {
2: char[] input11 = input1.toCharArray();
3: char[] input21 = input2.toCharArray();
4:
5: int len1 = input11.length, len2 = input21.length;
6:
7: int len = len1 > len2 ? len1 : len2;
8: int[] result = new int[len + 1]; // 结果数组
9:
10: int[] number1 = new int[len];
11: int[] number2 = new int[len];
12:
13: // 数据反转 因为下标的因素
14: for (int i = 0; i < len; i++) {
15: // input1长,input2补位0
16: if (len == len1) {
17: number1[i] = input11[len - i - 1] - '0';
18: if (i < len2) {
19: number2[i] = input21[len2 - i - 1] - '0';
20: } else
21: number2[i] = 0;
22: } else {
23: number2[i] = input21[len - i - 1] - '0';
24: if (i < len1) {
25: number1[i] = input11[len1 - i - 1] - '0';
26: } else
27: number1[i] = 0;
28: }
29: }
30:
31: //print(number1);
32: //print(number2);
33:
34: int count = 0;
35:
36: for (int i = 0; i < len; i++) {
37: result[i] += number1[i] + number2[i];
38: if (result[i] > 10) {
39: result[i + 1] = result[i] / 10; // 进位
40: result[i] = result[i] % 10;
41: }
42:
43: }
44:
45: if (result[len] != 0)
46: count = len;
47: else
48: count = len - 1;
49:
50: for (int i = count; i >= 0; i--) {
51: System.out.print(result[i]);
52: }
53: System.out.println();
54: }
55:
56: static void print(int[] number) {
57: for (int i = number.length - 1; i >= 0; i--) {
58: System.out.print(number[i]);
59: }
60: System.out.println();
61: }
62:
63: public static void main(String[] args) {
64: // TODO Auto-generated method stub
65:
66: System.out.println("测试数据");
67: String input11 = "1234332234456";
68: String input12 = "12352153262131236";
69:
70: String input21 = "120";
71: String input22 = "9";
72:
73: String input31 = "123";
74: String input32 = "18";
75:
76: plus(input11, input12);
77: plus(input21, input22);
78: plus(input31, input32);
79: }
80:
81: }
2、大整数相乘
1: static void multiply(String input1, String input2) {
2: char[] input11 = input1.toCharArray();
3: char[] input21 = input2.toCharArray();
4:
5: int len1 = input11.length, len2 = input21.length;
6:
7: int[] number1 = new int[len1];
8: int[] number2 = new int[len2];
9:
10: // 数据反转 因为下标的因素
11: for (int i = 0; i < len1; i++) {
12: number1[i] = input11[len1 - i - 1] - '0';
13: }
14:
15: for (int i = 0; i < len2; i++) {
16: number2[i] = input21[len2 - i - 1] - '0';
17: }
18:
19: //print(number1);
20: //print(number2);
21:
22: int[] result = new int[len1 + len2]; // 结果数组
23: int count = 0;
24:
25: for (int i = 0; i < len1; i++)
26: for (int j = 0; j < len2; j++) {
27: result[i + j] += number1[i] * number2[j];
28: if (result[i + j] > 10) {
29: result[i + j + 1] = result[i + j] / 10; // 进位
30: result[i + j] = result[i + j] % 10;
31: }
32: }
33:
34: if (result[len2 + len1-1] != 0)
35: count = len2 + len1 - 1;
36: else
37: count = len2 + len1 - 2;
38:
39: for (int i = count;i>=0; i--) {
40: System.out.print(result[i]);
41: }
42: System.out.println();
43: }
44:
45: static void print(int[] number) {
46: for (int i = number.length-1;i>=0; i--) {
47: System.out.print(number[i]);
48: }
49: System.out.println();
50: }
51:
52: public static void main(String[] args) {
53: // TODO Auto-generated method stub
54:
55: int i = 1;
56: int j = i++;
57: System.out.println(i + " " + j);
58: // System.out.println(i > j++);
59: if ((i > j++) && (i++ == j))
60: i += j;
61: System.out.println(i + " " + j);
62: System.out.println((int) 'a');
63: System.out.println('0' + 1);
64:
65: System.out.println("测试数据");
66: String input11 = "1234332234456";
67: String input12 = "12352153262131236";
68:
69: String input21 = "120";
70: String input22 = "9";
71:
72: String input31 = "123";
73: String input32 = "2";
74:
75: String input41 = "100";
76: String input42 = "200";
77:
78: multiply(input11,input12);
79: multiply(input21, input22);
80: multiply(input31, input32);
81: multiply(input41, input42);
82: }
3、求一个整数n的阶乘,0 <= n <=5000。
比如n = 50,结果为30414093201713378043612608166064768844377641568960512000000000000。
......
4、一个是100!估算要多少个bit位来表示
解题思路:如果找数学公式的话就陷入了思维误区。比如说最简单的4!和5!来分析。
先来分析:2^4=16<4!=24<2^5=32,即4!可以用5bit进行表示。11000。
同理:2^6=64<5!=120<2^7=128,即5!的阶乘可以用7bit表示。1111000
可以通过分析知,这里最主要的是获取k!的范围空间,但阶乘的范围不好确定。由于估算求bit位,可以想到与2有关,所以这里联想到使用取对数,将阶乘中的乘法转换成简单的加法运算进行计算。
经过转换,4!和5!情况如下。
1) 记M=lg4! = lg4+lg3+lg2。M的范围
M<lg4+lg4+lg2=5且M>lg4+lg2+lg2=4
即4!用bit表示:位数在区间[4,5]之间。
2) 记N=lg4! = lg5+lg4+lg3+lg2。N的范围
N<lg8+lg4+lg4+lg2=8且N>lg4+lg4+lg2+lg2=6
即5!用bit表示:位数在区间[6,8]之间。
根据取对数之后的计算,可以大致估计出阶乘后的范围。
所以,对于100!而言,T=lg100+lg99+..+lg64+..+lg32+..+lg16+..+lg8+..+lg4+..+lg2,通过上述类似的估计范围可得:
T>lg64+lg64+...+lg64+lg32+..+lg32+lg16+..+lg16+lg8+..+lg8+lg4+..+lg4+lg2+..+lg2 = 37*lg64+32*lg32+16*lg16+8*lg8+4*lg4+2*lg2+1*lg1 = 480
T<lg128+lg128+...+lg128+lg64+..+lg64+lg32+..+lg32+lg16+..+lg16+lg8+..+lg8+lg4+..+lg4+lg2 = 36*lg128+32*lg64+16lg32+8lg16+4lg8+2lg4+lg2= 573。
所以最后的范围就在480~573之间 。
5、找符合条件的整数(来源自编程之美)
任意给定一个正整数N,求一个最小的正整数M(M>1),使得M*N的十进制表示形式中只含有1和0。
1: #include<iostream>
2: using namespace std;
3:
4: int find_M(int N) {
5: // 边界条件
6: if(N == 1)
7: return 1;
8: // 初始化余数数组
9: int *A = new int[N]; // 记录已有的余数,A[i]表示对N的余数为i的最小满足条件的数值
10: int *B = new int[N]; // 记录更新的余数
11: memset(A, -1, N*sizeof(int));
12: A[1] = 1;
13: // 寻找过程
14: int factor = 10;
15: bool not_found = true;
16: while(not_found) {
17: memset(B, -1, N*sizeof(int));
18: int x = factor % N; // 高位数值对N的余数
19: // 高位数值 + 0 的情况
20: if(A[x] == -1) {
21: B[x] = factor;
22: if(x == 0)
23: break;
24: }
25: // 高位数值 + 低位正整数的情况
26: for(int i=1; i<N; i++) { // 遍历每个可能的余数
27: if(A[i] == -1)
28: continue;
29: int new_x = (x + i) % N; // 计算出的余数
30: if(A[new_x] == -1 && B[new_x] == -1) { // 如果是一个新的余数,保存
31: B[new_x] = factor + A[i];
32: if(new_x == 0) { // 刚好找到的新的余数是0
33: not_found = false;
34: break;
35: }// if
36: }// if
37: }//for
38: factor *= 10;
39: for(int j=0; j<N; j++) {
40: if(A[j]==-1 && B[j]!=-1) {
41: A[j] = B[j];
42: }
43: }
44: }// while
45: int result = B[0];
46: delete[] A;
47: delete[] B;
48: return result;
49: }
50:
51: int main() {
52: int N;
53: while(true) {
54: cout << "N:";
55: cin >> N;
56: if(N < 1)
57: break;
58: cout << "M:" << find_M(N)/N << endl;
59: cout << "正整数为:" << find_M(N)/N*N << endl;
60: }
61: system("PAUSE");
62: return 0;
63: }
扩展资料: