poj1002

变态高精度乘法,难度在于各种奇奇怪怪的输入,去年好像改了七八天才过,要是比赛有这题肯定是各队的一道大坎
算法难度不大,编程难度极大

#include <stdio.h>
int mark=0,lock=0;
void check(char s1[])
{
    int len,i;
    len=strlen(s1);
    i=0;
    while (s1[i]=='0') i++;
    //printf("%d %d\n",i,len);
    if (i==len) s1[1]='\0';
}
void make(char s1[])
{
    int len;
    len=strlen(s1)-1;
    while (s1[len]=='0')
    {
        s1[len]='\0';
        len--;
    }
}
void finddot(char s1[])
{
    int len,i,j;
    len=strlen(s1);
    i=0;
    while (s1[i]!='.'&&i<=len) i++;
    if (i!=len+1)
    {
        mark=len-1-i;
       // printf("mark:%d  len:%d  i:%d\n",mark,len,i);
        lock=1;
        for (j=i;j<=strlen(s1);j++) s1[j]=s1[j+1];
    }
    else mark=0;
}
void mult(char s1[],char s2[])
{
    int a[1000],b[1000],c[1000];
    int len1,len2,i,j,len3;
   len1=strlen(s1);
   len2=strlen(s2);
   for (i=0; i<=len1+len2; i++)
   {
       a[i]=0; b[i]=0; c[i]=0;
   }
   for (i=0; i<=len1-1;i++)
    {
     a[len1-1-i]=s1[i]-'0';
    }

   for (i=0; i<=len2-1;i++)
    {
     b[len2-1-i]=s2[i]-'0';
    }

    len3=len1+len2;
    for (i=0;i<=len1-1;i++)
      for (j=0;j<=len2-1;j++)
  {
  c[i+j]=c[i+j]+a[i]*b[j];
  }

    for (i=0;i<=len3;i++)
    {
        c[i+1]=c[i+1]+c[i]/10;
        c[i]=c[i]%10;
    }

   while (c[len3]==0)len3=len3-1;
   for (i=len3;i>=0;i--) s1[len3-i]=c[i]+'0';
   s1[len3+1]='\0';
    return 0;
}
int main()
{
    char s[1000],s1[1000],s2[1000];
    int n,i,dot=0,len;
    while(scanf("%s %d",&s1,&n)!=EOF)
     {
        mark=0;
      make(s1);
    finddot(s1);
    check(s1);
    if (s1[0]=='\0') s1[0]='0';
   //  puts(s1);
 for (i=0;i<=strlen(s1);i++) s2[i]=s1[i];

    dot=mark;
    for (i=1;i<=n-1;i++)
    {
        mult(s1,s2);
        dot=dot+mark;
    }
    //  printf("%d\n",dot);

      if (dot>=strlen(s1))
      {
          printf(".");
          for (i=1;i<=(dot-strlen(s1));i++) printf("0");
           puts(s1);
       }
       else
       {
           len=strlen(s1);
          //   printf("%d\n",len);
        //     for (i=0;i<len;i++) printf("%c\n",s1[i]);
            for (i=0;i<len-dot;i++) printf("%c",s1[i]);
            if (dot!=0) printf(".");
            for (i=len-dot;i<len;i++) printf("%c",s1[i]);
       }
      printf("\n");
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值