【编程小题目7】求s=a+aa+aaa+aaaa+aa...a的值

题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),
几个数相加有键盘控制

 

#include <iostream>
#include <string>
using namespace std;

long Function(int number, int n)
{
  if(n == 1)
  {
    return number;
  }
  else
  {
    return Function(number ,n - 1) * 10 + number;
  }
}

int main()
{
  int Count, Number;
  long Sum = 0;
  cin >> Number >> Count;
  cout << "Sum = ";
  for(int i = 1; i <= Count; i++)
  {
    int Temp = Function(Number, i);
    if(i != Count)
    {
      cout << Temp << " + ";
    }
    else
    {
      cout << Temp << " = ";
    }
    Sum += Temp;
  }

  cout << Sum << endl;

  return 0;
}

你可能感兴趣的:(【编程小题目7】求s=a+aa+aaa+aaaa+aa...a的值)