码迷,mamicode.com
首页 > 其他好文 > 详细

澡柜编号 去带4的澡柜

时间:2017-12-07 00:44:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:span   输出   method   number   rgs   sys   去掉   一点   包含   

描述: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"); }

 

澡柜编号 去带4的澡柜

标签:span   输出   method   number   rgs   sys   去掉   一点   包含   

原文地址:http://www.cnblogs.com/xiandong/p/7995181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!