zoukankan      html  css  js  c++  java
  • 空位補零,你會選擇哪種方式?

    隨便寫著玩的。。。

    using System;

    namespace KiloNet
    {
        
    class  test
        {
            
    static void Main(string[] args)
            {
                
    //空位補零,你會選擇哪種方式?

                Console.WriteLine(
    "pad('1',4)={0}",pad("1",4));
                Console.WriteLine(
    "pad('12',4)={0}",pad("12",4));
                Console.WriteLine(
    "pad('123',4)={0}",pad("123",4));
                Console.WriteLine(
    "pad('1234',4)={0}",pad("1234",4));
                Console.WriteLine(
    "pad('12345',4)={0}",pad("12345",4));
                Console.WriteLine(
    "pad('123456',4)={0}",pad("123456",4));

                Console.WriteLine(
    "pad2('1',4)={0}",pad2("1",4));
                Console.WriteLine(
    "pad2('12',4)={0}",pad2("12",4));
                Console.WriteLine(
    "pad2('123',4)={0}",pad2("123",4));
                Console.WriteLine(
    "pad2('1234',4)={0}",pad2("1234",4));
                Console.WriteLine(
    "pad2('12345',4)={0}",pad2("12345",4));
                Console.WriteLine(
    "pad2('123456',4)={0}",pad2("123456",4));

                Console.WriteLine(
    "pad3('1',4)={0}",pad3("1",4));
                Console.WriteLine(
    "pad3('12',4)={0}",pad3("12",4));
                Console.WriteLine(
    "pad3('123',4)={0}",pad3("123",4));
                Console.WriteLine(
    "pad3('1234',4)={0}",pad3("1234",4));
                Console.WriteLine(
    "pad3('12345',4)={0}",pad3("12345",4));
                Console.WriteLine(
    "pad3('123456',4)={0}",pad3("123456",4));

                Console.Read();
            }

            
    //正常循環
            static string pad(string num, int n) {
                
    int i = num.Length;
                
    while(i++ < n) num = "0" + num;
                
    return num;
            }
            
            
    //遞歸
            static string pad2(string num, int n) {
                
    return (num.Length >= n)? num:pad2("0" + num, n);
            }

            
    //一次搞定
            static string pad3(string num, int n) {
                
    return ("".PadLeft(n, '0'+ num).Substring(n+ num.Length - Math.Max(num.Length, n), Math.Max(num.Length, n));
            }

        }
    }

    id 博主 = [[KILONET.CNBLOGS.COM alloc] initWithValue:@"天堂向右,我依然向左"

                  网名:@"老舟"

                  兴趣:@"影音,阅读"

                  动态:@"系统架构设计,Android通信模块开发"

                  网址:@"http://kilonet.cnblogs.com"
                  签名:@"--------------------------------------------------

                                  Stay Hungry , Stay Foolish

                                  求  知  若  渴,处  事  若  愚

                              --------------------------------------------------"

                  ];         // Never Release

  • 相关阅读:
    .NetCore Grpc 客服端 工厂模式配置授权
    DOCKER 拉取 dotnet 镜像太慢 docker pull mcr.microsoft.com too slow
    Introducing .NET 5
    VSCode 出现错误 System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached.
    Omnisharp VsCode Attaching to remote processes
    zookeeper3.5.5 centos7 完全分布式 搭建随记
    Hadoop2.7.7 centos7 完全分布式 配置与问题随记
    MySQL索引 索引分类 最左前缀原则 覆盖索引 索引下推 联合索引顺序
    SQL基础随记3 范式 键
    MySQL调优 优化需要考虑哪些方面
  • 原文地址:https://www.cnblogs.com/KiloNet/p/1558840.html
Copyright © 2011-2022 走看看