zoukankan      html  css  js  c++  java
  • 一道淘汰85%面试者的百度开发人员面试题

    这个是题目网址:http://student.csdn.net/mcd/topic/235300/753730


    注意:

    加上了9L@jetvster的代码,非常奇妙,第一眼看以为还不怎么样,结果挺牛的

    感觉7L@SeeMore91,8L哥们的思路挺好的,学习一下下生气

    刚才看到3L的回复,在此声明,以下的仅仅是我自己的代码与思路,请不要以为这是解说,仅仅是上个代码请看一下而已,放上来为的求个指导&指点

    另外,输出语句仅仅是为了測试结果的,请无视!假设须要全然满足题意,将代码第26行改动为: System.out.println(txt); 再在switch加上default部分,语句就一个continue,直接放弃本轮运行下一次循环

    本来一看感觉非常easy,可是写了一会儿之后越来越感觉不是那么简单,哭,于是我就充分利用咱们Java面向对象的特性,写完了。


    上代码(感觉差点儿相同是这样了,有错漏的地方还希望前辈们指正一下大笑

    PS:刚查了下百科,原来正整数是大于0的整数,所以,代码改一下

    /**
     * 依序遍历0到100闭区间内全部的正整数,
     * 假设该数字能被3整除,则输出该数字及‘*’标记;
     * 假设该数字能被5整除,则输出该数字及‘#’标记;
     * 假设该数字既能被3整除又能被5整除,则输出该数字及‘*#’标记。
     */
    public class Print {
        public static void main(String[] args){
            new Print().prints();
        }
        private void prints(){
            for(int i = 1;i <= 100;i++){
                String txt = "";
                int flag = 0;
                if (op3(i)){
                    flag += 1;
                }
                if (op5(i)){
                    flag += 2;
                }
                switch (flag){
                    case 1:txt = "*";break;
                    case 2:txt = "#";break;
                    case 3:txt = i+"#";break;
                }
                System.out.println("当前数字:" + i + "---->" + txt);
            }
        }
    
        private boolean op3(int i){
            if (i % 3 == 0) {
               return true;
            }
            return false;
        }
    
        private boolean op5(int i) {
            if (i % 5 == 0) {
                return true;
            }
            return false;
        }
    }

    输出:

    当前数字:1---->
    当前数字:2---->
    当前数字:3---->*
    当前数字:4---->
    当前数字:5---->#
    当前数字:6---->*
    当前数字:7---->
    当前数字:8---->
    当前数字:9---->*
    当前数字:10---->#
    当前数字:11---->
    当前数字:12---->*
    当前数字:13---->
    当前数字:14---->
    当前数字:15---->15#
    当前数字:16---->
    当前数字:17---->
    当前数字:18---->*
    当前数字:19---->
    当前数字:20---->#
    当前数字:21---->*
    当前数字:22---->
    当前数字:23---->
    当前数字:24---->*
    当前数字:25---->#
    当前数字:26---->
    当前数字:27---->*
    当前数字:28---->
    当前数字:29---->
    当前数字:30---->30#
    当前数字:31---->
    当前数字:32---->
    当前数字:33---->*
    当前数字:34---->
    当前数字:35---->#
    当前数字:36---->*
    当前数字:37---->
    当前数字:38---->
    当前数字:39---->*
    当前数字:40---->#
    当前数字:41---->
    当前数字:42---->*
    当前数字:43---->
    当前数字:44---->
    当前数字:45---->45#
    当前数字:46---->
    当前数字:47---->
    当前数字:48---->*
    当前数字:49---->
    当前数字:50---->#
    当前数字:51---->*
    当前数字:52---->
    当前数字:53---->
    当前数字:54---->*
    当前数字:55---->#
    当前数字:56---->
    当前数字:57---->*
    当前数字:58---->
    当前数字:59---->
    当前数字:60---->60#
    当前数字:61---->
    当前数字:62---->
    当前数字:63---->*
    当前数字:64---->
    当前数字:65---->#
    当前数字:66---->*
    当前数字:67---->
    当前数字:68---->
    当前数字:69---->*
    当前数字:70---->#
    当前数字:71---->
    当前数字:72---->*
    当前数字:73---->
    当前数字:74---->
    当前数字:75---->75#
    当前数字:76---->
    当前数字:77---->
    当前数字:78---->*
    当前数字:79---->
    当前数字:80---->#
    当前数字:81---->*
    当前数字:82---->
    当前数字:83---->
    当前数字:84---->*
    当前数字:85---->#
    当前数字:86---->
    当前数字:87---->*
    当前数字:88---->
    当前数字:89---->
    当前数字:90---->90#
    当前数字:91---->
    当前数字:92---->
    当前数字:93---->*
    当前数字:94---->
    当前数字:95---->#
    当前数字:96---->*
    当前数字:97---->
    当前数字:98---->
    当前数字:99---->*
    当前数字:100---->#
    
    Process finished with exit code 0
    


    9L的代码,好不easy看明确,改动了一下下,更符合题意

    public class Test {
        public static void main(String[] args){
            int max = 100;
            int flag3 = 1;      // 正整数不包含0,所以从1開始计数
            int flag5 = 1;
            int sum3 = 0;       // i%3 == 0 && i%15 == 0
            int sum5 = 0;       // i%5 == 0
            while (sum3 < max || sum5 < max) {
                sum3 = flag3 * 3;
                sum5 = flag5 * 5;
                if (sum3 < sum5) {
                    System.out.println(sum3 + ":*");
                    flag3++;
                } else if (sum3 > sum5) {
                    System.out.println(sum5 + ":#");
                    flag5++;
                } else {
                    System.out.println(sum3 + ":*#");
                    flag5++;
                    flag3++;
                }
            }
        }
    }
    
    输出是这种(不要纠结,想要全然符合题意,仅仅须要改动相应的sout方法,这里仅仅是为了測试结果好看一点)

    3:*
    5:#
    6:*
    9:*
    10:#
    12:*
    15:*#
    18:*
    20:#
    21:*
    24:*
    25:#
    27:*
    30:*#
    33:*
    35:#
    36:*
    39:*
    40:#
    42:*
    45:*#
    48:*
    50:#
    51:*
    54:*
    55:#
    57:*
    60:*#
    63:*
    65:#
    66:*
    69:*
    70:#
    72:*
    75:*#
    78:*
    80:#
    81:*
    84:*
    85:#
    87:*
    90:*#
    93:*
    95:#
    96:*
    99:*
    100:#





  • 相关阅读:
    【报错】ES报错找不到Gson类
    【报错】Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
    【JUC】AtomicInteger源码
    【Netty】Netty服务启动源码
    【Netty】Netty实现简单RPC
    【Netty】心跳机制
    【Netty】Netty模型
    【Netty】Reactor模型
    C# 好狂的多线程呀
    select使用
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/3872014.html
Copyright © 2011-2022 走看看