使用C中的缩进打印

时间:2016-06-24 03:48:28

标签: c algorithm printf indentation

哪种方式最适合排列右边的所有输出?

示例:for powers of 2:

class GameScene: SKScene {

    @objc func slide(_ sender: UISwipeGestureRecognizer){
        print("WORKS")
    }
    override func didMove(to view: SKView) {
        let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(slide))
        upSwipe.direction = .up
        view.addGestureRecognizer(upSwipe)
    }
}

如果我的最后一个号码有N个数字,那么如何打印第一个号码(在这种情况下只有一个数字),左边有(N-1)个空格?

3 个答案:

答案 0 :(得分:4)

您可以使用printf()*提供计算字段宽度。因此,在计算出最大位数后,您可以执行以下操作:

printf("%*d\n", max_digits, value);

答案 1 :(得分:2)

您可以使用printf的宽度技巧,如下所示:Printf reference

printf ("Width trick: %*d \n", 5, 10);

将产生

Width trick: 10

答案 2 :(得分:1)

实施例

But it seems they have differet way to show up, one of them is popup and another is change the css. If there is a way to combine them? I tried like

    $(function () {
    $('a[href="#search"]').on('click', function(event) {
        event.preventDefault();
        $('#search').addClass('open');
        $.mobile.pageContainer.pagecontainer("getActivePage")
            .addClass("blur-filter");
        $('#search > form > input[type="search"]').focus();
    });

    $('#search, #search button.close').on('click keyup', function(event) {
        if (event.target == this || event.target.className == 'close' || event.keyCode == 27) {
            $(this).removeClass('open');
            $(".blur-filter").removeClass("blur-filter");
        }
    });


    //Do not include! This prevents the form from submitting for DEMO purposes only!
    $('form').submit(function(event) {
        event.preventDefault();
        return false;
    })
});

输出

int main(int argc, char *argv[]) {
    for (int i=2; i<134217729;i=i*2)
        printf("%*d\n",20, i);
    return 0;
}