zoukankan      html  css  js  c++  java
  • 澡柜编号 去带4的澡柜

    描述:Mostrp是个爱干净的好少年。 有一次去澡堂洗澡时发现 澡堂的澡柜编号中没有出现过数字‘4’。

    Mostrp 感到很好奇。可能是因为在澡堂老板眼里。数字‘4’是十分不吉利的。现在Mostrp知道澡柜的最大的编号N,你能帮他算出澡堂一共有多少澡柜吗?

    输入:输入一个N。( 1 <= N <= 50000 )

    输出:输出澡柜的个数。

    ===================================================================================

    去掉包含4的数剩下的就是要求的

    class QuSi{
        public static int qu(int n){
            int count = n;    //最终需要的数量
    
            String[] arr = new String[n];
            //0-n所有的澡柜号码转成字符串数组
            for (int i = 0; i < n; i++) {
                arr[i]=(i+1)+"";
            }
    
            //判断数组中含有4的字符串,-1
            for (int i = 0; i < arr.length; i++) {
                if(arr[i].contains("4")){
                    count--;
                }
            }
            
            return count;
        }
    }

    简化一点的方法

    定一个方法,判断数字是不是含有4,

    for循环从1到输入数字,调用方法判断是不是带4 ,不是的++

    最终输出

    public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            int n=20;
            
            int count=0;
            for(int i=1;i<=n;i++){
                if(!isContainsNumber4(i)){
                    //System.out.print(i+" ");
                    count++;
                }
            }
            System.out.println("澡柜共有:"+count+"个");
        }
    public static boolean isContainsNumber4(int x){ return (x+"").contains("4"); }
  • 相关阅读:
    使用子查询可提升 COUNT DISTINCT 速度 50 倍
    页面装载js及性能分析方法
    用CSS创建打印页面
    每个Web开发者都应该知道的关于URL编码的知识
    C IO programming test code
    全球NTP服务器列表
    MySQL数据的查询注意
    Python使用pyMysql模块插入数据到mysql的乱码解决
    单元测试
    python threading.thread
  • 原文地址:https://www.cnblogs.com/xiandong/p/7995181.html
Copyright © 2011-2022 走看看