習(xí)題6-2 使用函數(shù)求特殊a串?dāng)?shù)列和 (20 分)
1. 題目摘自
https://pintia.cn/problem-sets/12/problems/309
2. 題目?jī)?nèi)容
給定兩個(gè)均不超過 9
的正整數(shù) a
和 n
蜈首,要求編寫函數(shù)求 a+aa+aaa++?+aa?a(n個(gè)a)
之和退腥。
函數(shù)接口定義:
int fn( int a, int n );
int SumA( int a, int n );
其中函數(shù) fn
須返回的是 n
個(gè) a
組成的數(shù)字杠览;SumA
返回要求的和乐纸。
輸入樣例:
2 3
輸出樣例:
fn(2, 3) = 222
s = 246
3. 源碼參考
#include<iostream>
#include<math.h>
using namespace std;
int fn(int a, int n);
int SumA(int a, int n);
int main()
{
int a, n;
cin >> a >> n;
cout << "fn(" << a << ", " << n << ") = " << fn(a, n) << endl;
cout << "s = " << SumA(a, n) << endl;
return 0;
}
int fn(int a, int n)
{
int s = 0;
while (n)
{
s += a * pow(10, n - 1);
n--;
}
return s;
}
int SumA(int a, int n)
{
int s = 0;
while (n)
{
s += fn(a, n);
n--;
}
return s;
}