| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 11582 人关注过本帖
标题:输入两个正整数a和n,求a+aa+aaa+…+aa…a(n个a)之和
只看楼主 加入收藏
Lyz贞贞
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-4-14
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
输入两个正整数a和n,求a+aa+aaa+…+aa…a(n个a)之和
用c#求:输入两个正整数a和n,求a+aa+aaa+…+aa…a(n个a)之和。例如输入2和3,输出246(2+22+222)。

我做的结果出现了很多错误,求方法.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _9
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入a的值:");
            string input = Console.ReadLine();
            int n = int.Parse(input);
            Console.Write("请输入n的值:");
            string input = Console.ReadLine();
            int n = int.Parse(input);
            int i = 0;
            int a = 0;
            int sum = 0;
            for (i = 1; i < n + 1; i++)
                sum = sum + i;
            i = i * 10 + a;
            Console.WriteLine("{0}+{1}={2}\t", a, i, i + a);

        }
    }
}
搜索更多相关主题的帖子: 正整数 
2013-04-14 15:35
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:7 
static void Main(string[] args)
        {
            int i = 0;
            int a = 0;
            int n = 0;
            Console.Write("请输入a的值:");
            string input = Console.ReadLine();
            a = int.Parse(input);
            Console.Write("请输入n的值:");
            input = Console.ReadLine();
            n = int.Parse(input);
            
            int sum = 0;
            int temp = 0;
            string str = "";
            for (i = 0; i < n; i++)
            {
                temp = temp * 10 + a;
                sum = sum + temp;
                str += temp + "+";
            }
            if (str.Length > 1)
            {
                str = str.Substring(0, str.Length - 1);
            }
            Console.WriteLine(str + "=" + sum);
            Console.ReadKey();
        }
收到的鲜花
  • Lyz贞贞2013-04-14 23:21 送鲜花  2朵   附言:太感谢你了,我觉得你的方法很好!对于新手 ...
2013-04-14 16:11
yctchxf
Rank: 6Rank: 6
来 自:盐城
等 级:侠之大者
威 望:2
帖 子:176
专家分:454
注 册:2012-4-10
收藏
得分:5 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eg4
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, n,b,sum=0;
            Console.WriteLine("输入两个数 ");
            a = int.Parse(Console.ReadLine());
            n = int.Parse(Console.ReadLine());
            b = a;
            for (int i = 0; i < n; i++)
            {
                sum += b;
                b = b * 10 + a;
            }
            Console.Write("sum={0}",sum);
            Console.ReadKey();
        }
    }
}
记得给分……
收到的鲜花
  • Lyz贞贞2013-04-14 23:26 送鲜花  1朵   附言:谢谢你,答案是对的,但是后面有些欠佳,没 ...
2013-04-14 16:42
昏昏玉碎
Rank: 1
等 级:新手上路
帖 子:3
专家分:8
注 册:2013-4-10
收藏
得分:8 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _99
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, n, i, b = 0, sum = 0;
            Console.WriteLine("请输入a的值:");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入n的值:");
            n = int.Parse(Console.ReadLine());
            for (i = 0; i < n-1; i++)
            {

                b = b * 10 + a;
                Console.Write("{0}+", b);
                sum += b;
            }
            b = b * 10 + a;
            Console.Write("{0}", b);
            sum += b;
            Console.Write("={0}", sum);
            Console.ReadLine();
        }
    }
}

我刚入门,用的是比较笨的方法......
不过上面这个我调试了,是对的。
收到的鲜花
  • Lyz贞贞2013-04-14 23:24 送鲜花  3朵   附言:太感谢你了,我觉得你的方法很好!和我的方 ...
2013-04-14 18:42
Lyz贞贞
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-4-14
收藏
得分:0 
回复 2楼 yhlvht
太感谢你了,我觉得你的方法很好!对于新手的我来说,后面的部分,那个方法没有学过,不太懂!
2013-04-14 23:14
nbwbccn
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-5-4
收藏
得分:0 
Console.WriteLine("计算a+aa+aaa+...+aaa...aaa(n个a)的值\n");
            Console.Write("请输入a的值:");
            string input = Console.ReadLine();
            int a = Int32.Parse(input);
            Console.Write("请输入n的值:");
            string numstr = Console.ReadLine();
            int number = Int32.Parse(numstr);
            string tempstr = null;
            int tempValue = 0;
            int result = 0;
            for (int j = 0; j < number; j++)
            {
                for (int i = 0; i <j+1; i++)
                    tempstr += "1";               
                tempValue += Int32.Parse(tempstr);
                tempstr =null;
            }
            result = a * tempValue;
            Console.Write(result);
            Console.ReadKey();
2013-05-04 09:41
快速回复:输入两个正整数a和n,求a+aa+aaa+…+aa…a(n个a)之和
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026778 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved