Posted in 面试题 onJuly 22, 2012
void Multiple(char A[], char B[], char C[]) {
int TMP, In=0, LenA=-1, LenB=-1;
while(A[++LenA] != ‘\0′);
while(B[++LenB] != ‘\0′);
int Index, Start = LenA + LenB – 1;
for(int i=LenB-1; i>=0; i–) {
Index = Start–;
if(B[i] != ’0′) {
for(int In=0, j=LenA-1; j>=0; j–) {
TMP = (C[Index]-’0′) + (A[j]-’0′) * (B[i] – ’0′) + In;
C[Index--] = TMP % 10 + ’0′;
In = TMP / 10;
}
C[Index] = In + ’0′;
}
}
}
int main(int argc, char* argv[]) {
char A[] = “21839244444444448880088888889″;
char B[] = “38888888888899999999999999988″;
char C[sizeof(A) + sizeof(B) - 1];
for(int k=0; k
C[k] = ’0′;
C[sizeof(C)-1] = ‘\0′;
Multiple(A, B, C);
for(int i=0; C[i] != ‘\0′; i++)
printf(“%c”, C[i]);
}
int TMP, In=0, LenA=-1, LenB=-1;
while(A[++LenA] != ‘\0′);
while(B[++LenB] != ‘\0′);
int Index, Start = LenA + LenB – 1;
for(int i=LenB-1; i>=0; i–) {
Index = Start–;
if(B[i] != ’0′) {
for(int In=0, j=LenA-1; j>=0; j–) {
TMP = (C[Index]-’0′) + (A[j]-’0′) * (B[i] – ’0′) + In;
C[Index--] = TMP % 10 + ’0′;
In = TMP / 10;
}
C[Index] = In + ’0′;
}
}
}
int main(int argc, char* argv[]) {
char A[] = “21839244444444448880088888889″;
char B[] = “38888888888899999999999999988″;
char C[sizeof(A) + sizeof(B) - 1];
for(int k=0; k
C[sizeof(C)-1] = ‘\0′;
Multiple(A, B, C);
for(int i=0; C[i] != ‘\0′; i++)
printf(“%c”, C[i]);
}
大整数数相乘的问题
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@