V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
aheadlead
V2EX  ›  问与答

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

  •  
  •   aheadlead · 2016-09-22 16:21:47 +08:00 · 1047 次点击
    这是一个创建于 2784 天前的主题,其中的信息可能已经有所发展或是发生改变。
    刚二面滴滴的时候,面试老师出了一道如标题的题目。
    面试的时候比较慌张,跟着老师的引导就只答出了递归的方法。

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

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

    更多解法: https://github.com/gatieme/CodingInterviews/tree/master/046-求1%2B2%2B3%2B...%2Bn
    1 条回复    2016-09-22 21:44:14 +08:00
    tchekai704
        1
    tchekai704  
       2016-09-22 21:44:14 +08:00 via iPhone
    茴香豆的 N 种写法?
    那么问题来了, N 是几?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   6179 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 02:52 · PVG 10:52 · LAX 19:52 · JFK 22:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.