输入a与n,求1/a + 2/aa + 3/aaa + ... + n/(n个a)

题目描述:输入an,求 1/a + 2/aa + 3/aaa + ... + n/(n个a)

C语言实现代码如下:

#include 
#include 

/*Question Description: input a and n, output 1/a + 2/aa + 3/aaa + ...... + n/(n consecutive a)*/
double calculate(int c, int a){ //计算并返回每一项的数值
    int i;
    double v;
    double mole = (c + 1);  //分子
    double deno = 0;
    for(i = 0; i <= c; ++i){ 
        deno += (a * pow(10, i)); //计算分母
    }
    v = mole / deno; 
    return v;
}

int main() {
    int a, n, i;
    double r = 0;
    scanf("%d%d", &a, &n);
    for(i = 0; i < n; ++i){
        r += calculate(i, a); //求和 n 次
    }
    printf("%lf\n", r);
    return 0;
}

你可能感兴趣的:(输入a与n,求1/a + 2/aa + 3/aaa + ... + n/(n个a))