user-0 提问: 怎么求两个大于100位的整数的和，求解决

回复:
user: user-1
content: 使用开源GNU高精度库GMP，支持整形、浮点  #include <gmpxx.h> #include <iostream> using namespace std; int main () {   mpz_class a, b, c;     a = 1234;   b = "-5678845738956723096583057390673675";   c = a*b;   cout << "a*b is " << c << "\n";   cout << "absolute value is " << abs(c) << "\n";     return 0; }   输出  a*b is -7007695641872596301183492820091314950 absolute value is 7007695641872596301183492820091314950

user: user-2
content: #include <stdio.h> #include <vector> using namespace std; #define MAX_LEN   10000  int main() { 	vector<int> vecA; 	vector<int> vecB; 	char c;  	printf("Please input the first number:\n");  	while(1) 	{ 		scanf("%c", &c); 		if ((c == '\n')||(c == '\r')) 			break; 		if ((c>'9')||(c<'0')) 		{ 			printf("Format Error\n"); 			return -1; 		} 		vecA.push_back(c-'0'); 	} 	/*for (int i =0; i<vecA.size(); i++) 	{ 		printf("%d", vecA[i]); 	}*/ 	printf("Please input the second number:\n"); 	while(1) 	{ 		scanf("%c", &c); 		if ((c == '\n')||(c == '\r')) 			break; 		if ((c>'9')||(c<'0')) 		{ 			printf("Format Error\n"); 			return -1; 		} 		vecB.push_back(c-'0'); 	} 	vector<int> vecSum; 	int Len = vecA.size()>vecB.size()? vecB.size():vecA.size(); 	int tempRes = 0; 	int tempGo = 0; 	for(int i = 0; i<Len; i++) 	{ 		tempRes = (vecA[vecA.size()-i-1]+vecB[vecB.size()-i-1]+tempGo)%10; 		tempGo = (vecA[vecA.size()-i-1]+vecB[vecB.size()-i-1]+tempGo)/10; 		vecSum.insert(vecSum.begin(), tempRes); 	} 	if (vecA.size() == Len) 	{ 		if (vecB.size() == Len) 		{ 			if (tempGo !=0) 			{ 				vecSum.insert(vecSum.begin(), tempGo); 			} 		} 		else 		{ 			for(int j=0; j<vecB.size()-Len; j++) 			{ 				tempRes = (vecB[vecB.size()-j-Len-1]+tempGo)%10; 				tempGo = (vecB[vecB.size()-j-Len-1231]+tempGo)/10; 				vecSum.insert(vecSum.begin(), tempRes);  			} 		}  	} 	else 	{ 		for(int j=0; j<vecA.size()-Len; j++) 			{ 				tempRes = (vecA[vecA.size()-j-Len-1]+tempGo)%10; 				tempGo = (vecA[vecA.size()-j-Len-1]+tempGo)/10; 				vecSum.insert(vecSum.begin(), tempRes);  			} 	}  	printf("Sum is:\n");     for (int i =0; i<vecSum.size(); i++) 	{ 		printf("%d", vecSum[i]); 	}   }

user: user-3
content: #include <stdio.h> #include <stdlib.h> #include <string.h>  #define MAX_NUM_LEN 2048  int cal_factorial(unsigned char *num_list, int num_size, int n);  int cal_val(unsigned char *num_list, int num_size, int base, int num) {     int i = 0, j;     int len = 1, tmp, carry_bit;      num_list[1] = 1;      while (i < num) {         carry_bit = 0;         for (j = 1; j <= len; j++) {             tmp = num_list[j] * base + carry_bit;             num_list[j] = tmp % 10;             carry_bit = tmp / 10;             if (j >= len && carry_bit != 0)                 len++;             if (num_size-1 < len) {/* overflow */                 printf("Overflow!\n");                 return num_size-1;             }         }         i++;     }      return len; }   int main(void) {     unsigned char num_list[MAX_NUM_LEN];     int num_len, i;     int base, power;      memset(num_list, 0, sizeof(num_list));     printf("Please input two numbers: ");     scanf("%d%d", &base, &power);      if (base < 0 && power % 2)         num_list[0] = 1; /* negative number */     else         num_list[0] = 0;     base = abs(base);      num_len = cal_val(num_list, MAX_NUM_LEN, base, power);     printf("len = %d\n", num_len);     if (num_list[0])         putchar('-');     for (i = num_len; i >= 1; i--)         printf("%d", num_list[i]);     printf("\n");     return 0; }   int cal_factorial(unsigned char *num_list, int num_size, int n) {     int carry_bit, i, j, len = 1;     int tmp;      num_list[1] = 1;     for (i = 2; i <= n; i++)     {         carry_bit = 0;         for (j = 1; j <= len; j++) {             tmp = num_list[j] * i + carry_bit;             num_list[j] = tmp % 10;             carry_bit = tmp / 10;             if (j >= len && carry_bit != 0)                 len++;             if (num_size -1 < len) { /* overflow */                 printf("Overflow!\n");                 return num_size-1;             }         }      }      /*     for (i = len; i >= 1; i--)//逆序输出数组就是结果         printf("%d", a[i]);     printf("\n");     */     return len; } 这个是大数乘法的算法，建议在这个基础上可以改成加法。

user: user-4
content: 仅供参考：#include <stdio.h> #include <string.h> #define MAXLEN 1000 char a1[MAXLEN]; char a2[MAXLEN]; static int v1[MAXLEN]; static int v2[MAXLEN]; static int v3[MAXLEN]; int i,j,n,L,z; void main(void) {     scanf("%d",&n);     for (j=0;j<n;j++) {         scanf("%s%s",a1,a2);          L=strlen(a1);         for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';          L=strlen(a2);         for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';          for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];          for (i=0;i<MAXLEN;i++) {             if (v3[i]>=10) {                 v3[i+1]+=v3[i]/10;                 v3[i]=v3[i]%10;             }         }          printf("Case %d:\n", j+1);         printf("%s + %s = ", a1, a2);          z=0;         for (i=MAXLEN-1;i>=0;i--) {             if (z==0) {                 if (v3[i]!=0) {                     printf("%d",v3[i]);                     z=1;                 }             } else {                 printf("%d",v3[i]);             }         }         if (z==0) printf("0");          printf("\n");     } } //Sample Input //3 //0 0 //1 2 //112233445566778899 998877665544332211 // //Sample Output //Case 1: //0 + 0 = 0 //Case 2: //1 + 2 = 3 //Case 3: //112233445566778899 + 998877665544332211 = 1111111111111111110

user: user-5
content: 这个你得学会运用rand函数和srand函数: 这是我们C语言课本上的关于这两个函数的运用方法（附上一张图片 懒得拼QAQ）  这是我编译的代码和运行结果；挺简单的，你自己运行看看； #include<stdio.h> #include<stdlib.h> #include<time.h> int main() {     int i;     int a[2];     printf("请输入两个超过100位的数\n");     srand((int)time(0));     for(i=0;i<2;i++){     	a[i]=(int)(100+400*rand()/RAND_MAX);     	printf("%5d",a[i]); 	} 	printf("\n"); 	printf("请输入两个数之和\n"); 	int sum=0; 	for(i=0;i<2;i++){ 		sum=sum+a[i];  	} 	printf("%5d\n",sum); 	return 0; }

user: user-0
content: 我这个只能求同位的整数的和，怎么求随机位数整数的和