澡柜编号 去带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"); }

 

posted @ 2017-12-06 22:15  沃泽法克  阅读(462)  评论(0编辑  收藏  举报