zoukankan      html  css  js  c++  java
  • 判断0-N之间出现1的次数

                Console.WriteLine("请输入截止数字?退出请输入y");
                string input = Console.ReadLine();
                int n = Convert.ToInt32(input);
                int count = 0; //计数
                for (int i = 0; i <= n; i++)
                {
                    string str = i.ToString();//要匹配的字符串
                    string pattern="1"; //要匹配的正则表达式模式
                    MatchCollection matches = Regex.Matches(str, pattern);  //正则表达式匹配验证
                    count += matches.Count;  //获得匹配数量,加到计数上,
                    //foreach (Match NextMatch in matches) //遍历获得数量也行,不过效率低
                    //{
                    //    count++;
                    //}
                }
                Console.WriteLine("在0-{0}中1出现了"+count+"",input);
  • 相关阅读:
    开发day7
    开发day6
    开发day5
    开发day4
    开发day3
    开发day2
    开发day1
    假期学习2/8
    什么是栈帧
    JDK、JRE和JVM到底是什么
  • 原文地址:https://www.cnblogs.com/wdmhn/p/3166917.html
Copyright © 2011-2022 走看看