【程序18】
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.例如2+22+222+2222+22222(此时
共有5个数相加),几个数相加有键盘控制
1.程序分析:关键是计算出每一项的值.
2.程序源代码:
unsigned int getNTen( unsigned int numA, unsigned int numN)
{
if(numN == 0)
return numA;
unsigned int result = numA;
unsigned int temp = numA;
for(int i=1; i<numN; i++)
{
temp *= 10 ;
result += temp;
}
return result;
}
unsigned int getSum(unsigned numA, unsigned numN)
{
if(numA==0 || numN == 0)
return 0;
unsigned int buf[1024] = {0};
unsigned int idx = 0;
for(int i=1; i<=numN; i++)
{
cout<<getNTen(numA, i)<<endl;;
}
}
void main()
{
getSum(5, 9);
}