定义
初始化
将去小数点的原始数组复制
对应位数相乘,然后累加,进位处理
//说明:
比如:
1 2 3 4 5 6
0 0 0 7 8 9 //相乘(假设789是底数)
——————
外循环对 7 8 9 从个位到百位遍历
内循环对 1 2 3 4 5 6 从个位到百位遍历
乘积固定到一个新数组(元素较多)
即 9*6,9*5,9*4,9*3,9*2,9*1 ->从新数组0号开始存
再 8*6,……………………………………………,8*1 ->从新数组1号开始存(加上面的乘积)
………………………………略…………………………………………
加完进位。
确定左边第一个非0数和右边第一个非0数
打印
下面是全部代码
#include <stdio.h>
int main()
{
char a[6]={0};
int n,i,j,c;
int potsum;
int result[160],result_copy[160],a_copy[5];
int left,right;
while(scanf("%s %d",a,&n)==2)
{
left=-1;
right=-1;
memset(result,0,sizeof(result));
memset(result_copy,0,sizeof(result_copy));
memset(a_copy,0,sizeof(a_copy));
for(i=5,j=0;i>=0;i--)
{
if(a[i]=='.')
{
potsum=(5-i)*n;
}
else
{
a_copy[j]=result[j]=a[i]-'0';
j++;
}
}
while(n>=2)
{
for(i=0;i<5;i++)
{
if(result[j]==0)
{
continue;
}
c=a_copy[i]*result[j];
result_copy[i+j]+=c;
}
for(i=0;i<159;i++)
{
result_copy[i+1]+=result_copy[i]/10;
result_copy[i]=result_copy[i]%10;
}
for(j=0;j<160;j++)
{
result[j]=result_copy[j];
result_copy[j]=0;
}
n--;
}
//打印
for(i=0;i<potsum;i++)
{
if(result[i]!=0)
{
right=i;
break;
}
}
for(j=159;j>=potsum;j--)
{
if(result[j]!=0)
{
left=j;
break;
}
}
if(left!=-1)
{
for(i=left;i>=potsum;i--)
{
printf("%d",result[i]);
}
}
if(right!=-1)
{
printf(".");
for(i=potsum-1;i>=right;i--)
{
printf("%d",result[i]);
}
}
}
}