aheadlead
V2EX  ›  问与答

有趣的题目:求 1+2+3+...+n,不能用乘除法、for、while、if、else、switch、case 等关键字及条件判断语句( A?B:C)。

  •  
  •   aheadlead · Sep 22, 2016 · 1436 views
    This topic created in 3528 days ago, the information mentioned may be changed or developed.
    刚二面滴滴的时候,面试老师出了一道如标题的题目。
    面试的时候比较慌张,跟着老师的引导就只答出了递归的方法。

    不过面完折腾出一个新方法:

    ( (int)( &((uint8_t (*) [n])0)[1+n][0]) ) >> 1;

    更多解法: https://github.com/gatieme/CodingInterviews/tree/master/046-求1%2B2%2B3%2B...%2Bn
    1 replies    2016-09-22 21:44:14 +08:00
    tchekai704
        1
    tchekai704  
       Sep 22, 2016 via iPhone
    茴香豆的 N 种写法?
    那么问题来了, N 是几?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   973 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 22:17 · PVG 06:17 · LAX 15:17 · JFK 18:17
    ♥ Do have faith in what you're doing.