加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数相乘

发布时间:2020-12-14 04:07:56 所属栏目:大数据 来源:网络整理
导读:开始是试用书上的方法写一下,大数的乘法:把数以字符串的形式输入,在换为数组存储,在按位依次处理。 ? 那些俩个相乘的测试数据都还过了。 #include stdio.h #include stdlib.h #include string.h #define N 200 int a[N]={0},b[N]={0},c[2*N]={0}; char s

开始是试用书上的方法写一下,大数的乘法:把数以字符串的形式输入,在换为数组存储,在按位依次处理。

? 那些俩个相乘的测试数据都还过了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200
int a[N]={0},b[N]={0},c[2*N]={0};
char s1[N],s2[N],i,j,len;
int n,m;

void getdigits(int *a,char *s)
{
???? len = strlen(s);
???? for(i = 0; i < len; ++i){
?????????? a[len-1-i]= *(s+i)- '0';
???? }
}

void multiply(int *a,int *b,int *c)
{

???? for(i = 0; i < n; i++)
?????????? for(j = 0; j < m; j++){
????????????? c[i+j] +=a[i] * b[i];
?????????? }

???? for(i = 0; i < n+m; i++)
???? {
?????????? c[i+1] += c[i]/10;
?????????? c[i] = c[i]%10;

???? }
}

int main()
{
??? scanf("%s%s",s1,s2);

??? getdigits(a,s1);
??? n=i;
??? getdigits(b,s2);
??? m=i;
??? multiply(a,b,c);
??? j=m+n;
??? while( c[j] ==0 ) {
??????? j--;
??? }
??? //printf("%d ",j);
??? for(i =j;i >= 0; i--)
?????????? printf("%d",c[i]);
??? printf("n");
??? return 0;
}
开始在输出位数那里纠结了好久。最后证明开始想多了。

写完以后感觉用于那些简单的乘法还行,复杂点的就过不了,最后还是用了java

作了杭电1042:http://acm.hdu.edu.cn/showproblem.php?pid=1042

AC代码:

import java.math.BigInteger;
import java.util.Scanner;


public class Main {
?public static void main(String[] args) {
??
??int n;
??Scanner cin =new Scanner(System.in);
??while(cin.hasNext())
??{
???BigInteger ans=BigInteger.valueOf(1);
???n=cin.nextInt();
???for(int i=n;i>=2;i--)
???{
????ans=ans.multiply(BigInteger.valueOf(i));
???}
???System.out.println(ans);
??}
?}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读