zoukankan      html  css  js  c++  java
  • 编程实践65

    编程实践6-5

    课程元素类型 任务

    定义函数,计算a+aa+aaa+...+a…a,共n项,其中a、n由键盘输入,a是[1,9]间的数字。

    using System;
    
    namespace sj_6_5
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a, n;
    
                Console.Write("请输入A的值:\n");
                a = Convert.ToInt32(Console.ReadLine());
                Console.Write("请输入N的值:\n");
                n = Convert.ToInt32(Console.ReadLine());
    
                Console.WriteLine(js(a, n));
    
                Console.ReadKey();
            }
            public static int js(int x, int y)
            {
                int c;
                string d;
                c = x;
                d = "11";
                for (int i = 0; i < y - 1; i++)
                {
                    c = c + x * Convert.ToInt32(d);
                    d = d + "1";
                }
                return c;
            }
        }
    }

    .版本2

    using System;
    
    namespace sj_6_5
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a, n;
    
                Console.Write("请输入A的值:\n");
                a = Convert.ToInt32(Console.ReadLine());
                Console.Write("请输入N的值:\n");
                n = Convert.ToInt32(Console.ReadLine());
    
                Console.WriteLine(js(a, n));
    
                Console.ReadKey();
            }
            public static int js(int x, int y)
            {
                int a = 0,c = 0;
                
                for (int i = 0; i < y; i++)
                {
                    a = a * 10 + x;
                    c = c + a;
                }
                return c;
            }
        }
    }
  • 相关阅读:
    《构建之法》读书笔记⑤
    《构建之法》读书笔记④
    个人总结
    构建之法阅读笔记03
    构建之法阅读笔记02
    构建之法阅读笔记01
    第二阶段冲刺——seven
    第二阶段冲刺——six
    第二阶段冲刺——five
    第二阶段冲刺——four
  • 原文地址:https://www.cnblogs.com/Wzqa/p/2812344.html
Copyright © 2011-2022 走看看