设和式s(d,n)=0.d+0.dd+0.ddd+….+0.dd…d为n项同码d小数之和,其中第k项小数点后有连续k个数字
d(d=1,2,3,…,9).
输入整数d,n(1<=d<=9,1< n<3000),计算并输出同码小数和s(d,n)(精确到小数点后8位)
//循环法
#include"stdio.h"
int main()
{
int n, d,i;
double s=0, a=1,b;
printf("请输入整数d,n:");
scanf_s("%d,%d",&d,&n);
for (i = 1; i <= n; i++)
{
a = 0.1*a;
b = (1 - a) / 9;
s = s + b*d;
}
printf("s(d,n)=%.8lf",s);
}
//递归法
#include"stdio.h"
int main()
{
int d, n, j;
double t = 0, s = 0;
scanf_s("%d,%d", &d, &n);
for (j = 1; j <= n; j++)
{
t = t / 10 + (double)d / 10; //递推
s += t;
}
printf("%.8lf\n", s);
return 0;
}