推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
WKPlus
V2EX  ›  Python

单循环两行代码打印出三角形

  •  
  •   WKPlus · May 11, 2015 · 5487 views
    This topic created in 4195 days ago, the information mentioned may be changed or developed.

    在hackerrank上面看到一个蛮好玩的题目: https://www.hackerrank.com/contests/pythonist/challenges/python-quest-1

    想了半天没有什么好办法,最后用了一个比较“猥琐”的解法:

    for i in range(1,input()): 
        print [1,22,333,4444,55555,666666,7777777,88888888,999999999][i-1]
    

    题目的限制比较多:
    1. 只能两行代码,且第一行已经写好了
    2. 只能用一个循环
    3. 不能用string相关的操作

    有人有其他有趣的解法么?

    24 replies  •  2015-05-12 13:54:45 +08:00
    FastMem
    FastMem
    May 11, 2015
    1
    研究了 半天也不知道PHP怎么两行。。
    pathletboy
    pathletboy
    May 11, 2015
    2
    ```python
    for i in range(1,input()):
    print(("%d"%(i))*i)
    ```
    这个合格?
    phx13ye
    phx13ye
    May 11, 2015
    3
    print i * str(i)
    为什么我零分啊
    hahastudio
    hahastudio
    May 11, 2015❤️3
    4
    这个简单啊,这根本就不是编程题,这个是数学题,求数列通项
    print i * (10 ** i - 1) / 9

    我觉得我能解出来是因为我太年轻。。。
    wartime
    wartime
    May 11, 2015❤️2
    5
    print reduce(lambda x, y: x * 10 + y, [i] * i)
    nlimpid
    nlimpid
    May 11, 2015
    6
    print int('1'*i)*i
    imn1
    imn1
    May 11, 2015
    7
    @FastMem
    SPL迭代器或者可以,不过我都忘得差不多了
    oott123
    oott123
    May 11, 2015❤️1
    8
    print("▲")
    staticor
    staticor
    May 11, 2015
    9
    同5楼, 楼主做几套公务员行测数量的几个数列题目就知道思路了
    batman2010
    batman2010
    May 11, 2015
    10
    foreach my $i (1..9) { print $i x $i, "\n"; }
    mulog
    mulog
    May 11, 2015
    11
    @pathletboy
    @phx13ye
    @nlimpid

    Note Using anything related to string will give a score of 0.
    dingyaguang117
    dingyaguang117
    May 11, 2015
    12
    ```
    for i in xrange(1,10):
    print str(i)*i
    ```
    est
    est
    May 11, 2015
    13
    @mulog 惨!
    aec4d
    aec4d
    May 11, 2015
    14
    用近似欺骗的手段通过了Orz
    what = lambda x: 1 if x == 1 else pow(10, x-1) + what(x - 1);print(what(i)*i)
    YORYOR
    YORYOR
    May 11, 2015
    15
    @oott123 给跪了
    feiyuanqiu
    feiyuanqiu
    May 11, 2015
    16
    这个题有个问题,当 N >= 10 之后,应该怎么显示呢?
    是显示 10 个 10 ?(这样就不是三角形了,这一行有 20 个字符)
    或者其他样式显示?(这样整个规则跟前面的就不一致了)

    不考虑 N >= 10 的 php 代码:

    用字符串:
    for ($i=1; $i <= $_GET['N']; $i++)
    printf("%s<br>", str_repeat($i, $i));

    不用字符串:
    for ($i=1, $base = 1; $i <= $_GET['N']; $base+=pow(10, $i), $i++)
    printf("%s<br>", $i*$base);
    zonghua
    zonghua
    May 11, 2015 via iPhone
    17
    我发现我智商不够
    fangjinmin
    fangjinmin
    May 11, 2015
    18
    print str(i) * i
    zealot0630
    zealot0630
    May 11, 2015
    19
    print i * sum([10**j for j in range(0, i)])

    不懂为什么fail
    shuson
    shuson
    May 11, 2015
    20
    给我用单循环两行代码画个三角形
    我给你钱
    xjx0524
    xjx0524
    May 11, 2015
    21
    @feiyuanqiu 1≤N≤9
    ykshen
    ykshen
    May 12, 2015
    22
    for i in range(1,input()):
    print i * (10 ** i) / 9
    mianju
    mianju
    May 12, 2015
    23
    @ykshen 感觉你这个是正解
    tsingyi
    tsingyi
    May 12, 2015
    24
    @oott123 你赢了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3492 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 69ms · UTC 10:18 · PVG 18:18 · LAX 02:18 · JFK 05:18
    ♥ Do have faith in what you're doing.