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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _6._5
{
    class Program
    {
        static void Main(string[] args)
        {
            int s=0;
            Console.WriteLine("input a,n");
            int a = Int32.Parse(Console.ReadLine());
            int n = Int32.Parse(Console.ReadLine());
            int t = a;
            for (int i = 0; i < n; i++)
            {
                s = s + t;//总和
                t = t*10 + a;//加数
            }

            Console.WriteLine("s={0}", s);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42265608/article/details/89356633
今日推荐