当前位置:网站首页>Exercise 6-2 using functions to sum a special series of a numbers (20 points)

Exercise 6-2 using functions to sum a special series of a numbers (20 points)

2022-06-11 22:25:00 Xiaoyan y

Given that neither is more than 9 The positive integer a and n, Ask to write a function to find a+aa+aaa++⋯+aa⋯a(n individual a) The sum of the .

Function interface definition :

int fn( int a, int n );
int SumA( int a, int n );

The function fn What has to be returned is n individual a The numbers that make up ;SumA Return the requested and .

Sample referee test procedure :

#include <stdio.h>

int fn( int a, int n );
int SumA( int a, int n );

int main()
{
    int a, n;

    scanf("%d %d", &a, &n);
    printf("fn(%d, %d) = %d\n", a, n, fn(a,n));        
    printf("s = %d\n", SumA(a,n));    

    return 0;
}

/* Your code will be embedded here */

sample input :

2 3

sample output :

fn(2, 3) = 222
s = 246

int fn( int a, int n ){
    int i,sum=0;
    for(i=0;i<n;i++){
        sum+=a;
        a*=10;
    }
    return sum;
}
int SumA( int a, int n ){
    int i,count=0;
    for(i=1;i<=n;i++){
        count+=fn(a,i);
    }
    return count;
}

 

原网站

版权声明
本文为[Xiaoyan y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112218445093.html