C#练习题答案: 多少个零?【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

多少个零?【难度:2级】:

答案1:

using System;

public static class Kata
{
  public static long CountZeroes(int n)
  {
    if (n <= 1) return 0;
    
    return CountZeroes(n-1) + 9 * (long)Math.Pow(10, n-2) * (n - 1);
  }
}

答案2:

using System;
public static class Kata
{
  public static long CountZeroes(int n)
  {
    return n<2 ? 0 : long.Parse((n-2)+new string('8',n-2)+"9");
  }
}

答案3:

using System;
public static class Kata
{
  public static decimal CountZeroes(int n)
  {
      return Math.Round((decimal)((((9 * (n - 1)) - 1) * Math.Pow(10, n - 1)) + 1) / 9);
  }
}

答案4:

using System;

public static class Kata
{
  public static long CountZeroes(int n)
  {
    if (n <= 0)
            {
                return 0;
            }
            else if (n == 18)
            {
                return 1688888888888888889;
            }
            else if (n == 17)
            {
                return 158888888888888889;
            }
            else if (n == 16)
            {
                return 14888888888888889;
            }
            
            double counter = 0;
            for (double i = 2; i <= n; i++)
            {
                counter += 9 * (i - 1) * Math.Pow(10, (double)(i - 2));
            }
            
            return (long)counter;
  }
}

答案5:

using System;

public static class Kata
{
  public static long CountZeroes(int n)
  {
    long result = 0;
    for(int i = 1; i < n; i++){
      long multiplier = (long)Math.Pow(10, i - 1);
      result += multiplier * 9 * i;
    }
    return result;
  }
}

答案6:

public static class Kata
{
  public static long CountZeroes(int n)
  { 
      long c=0; for (int i=0;i<n;i++) c+=(long)System.Math.Pow(10,i-1)*9*i; return c;
  }
}

答案7:

public static class Kata
{
    public static long CountZeroes(int n)
    {
        if (n <= 1) return 0;
        return long.Parse(string.Concat(n - 2, new string('8', n - 2), '9'));
    }
}

答案8:

using System.Collections.Generic;
using System.Linq;
using System;
public static class Kata
{
  public static long CountZeroes(int n)
  {
  Console.Write(n);
    switch (n)
    {
    case 2:    return 9;
    case 5:    return 38889;
    case 6:    return 488889;
    case 7:    return 5888889;
    case 8:    return 68888889;
    case 9:    return 788888889;
    case 3:    return 189;
    case 4:    return 2889;
    case 10:    return 8888888889;
    case 11:    return 98888888889;
    case 12:    return 1088888888889;
    case 13:    return 11888888888889;
    case 14:    return 128888888888889;
    case 15:    return 1388888888888889;
    case 16:    return 14888888888888889;
    case 17:    return 158888888888888889;
    case 18:    return 1688888888888888889;
    default:    return 0;
    }
    }
}

答案9:

public static class Kata
{
  public static long CountZeroes(int n)
  {
    if (n <= 1) return 0;
    return long.Parse(string.Format("{0}{1}{2}", n-2, new string('8', n-2), 9));
  }
}

答案10:

using System.Linq;

public static class Kata
{
    public static long CountZeroes(int n)
    {
        if (n <= 1) return 0;
        
        var x = n - 2;
        var zeroes = x.ToString() + string.Concat(Enumerable.Repeat('8', x)) + "9";
        return long.Parse(zeroes);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。扩展(*):不使用第三个变量如何交换? 2. 用方法来实现:将上题封装一个方法来做,方法有两个参数分别为num1,num2,将num1与num2交换。提示:方法有两个参数n1,n2,在方法中将n1与n2进行交换,使用ref。(*) 3. 请用户输入一个字符串,计算字符串中的字符个数,并输出。 4. 用方法来实现:计算两个数的最大值。思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:使用可变参数,params)。 5. 用方法来实现:计算1-100之间的所有整数的和。 6. 用方法来实现:计算1-100之间的所有奇数的和。 7. 用方法来实现:判断一个给定的整数是否为“质数”。 8. 用方法来实现:计算1-100之间的所有质数(素数)的和。 9. 用方法来实现:有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。 10. 用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。 11. 用方法来实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。Math.Round() 12. 请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。 13. 有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:①请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。②扩展(*):统计出每个字符的出现次数。 14. 将字符串" hello world,你 好 世界 ! "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你 好 世界 !"。 15. 制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数,以及每个学生的姓名。效果如图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值