JavaScript-Find an algorithm? 0000000 sequence Increment

Source: Internet
Author: User

To raise a chestnut: 0000000 increments, the format is this:

000000100000020000003.....递增到100000010.....递增到1000000100.....递增到1000000100000

Imitating the answer of @g_koala_c, wrote the PHP version:

for ($i = 0; $i < 100; $i++) {     $zero = '';    $k = 7-strlen($i);    for ($j = $k; $j >0; $j--) {         $zero .= 0;    }    echo $zero.$i.'
';}

Reply content:

To raise a chestnut: 0000000 increments, the format is this:

000000100000020000003.....递增到100000010.....递增到1000000100.....递增到1000000100000

Imitating the answer of @g_koala_c, wrote the PHP version:

for ($i = 0; $i < 100; $i++) {     $zero = '';    $k = 7-strlen($i);    for ($j = $k; $j >0; $j--) {         $zero .= 0;    }    echo $zero.$i.'
';}

JS notation, the principle is to first calculate the length of the number, and then 0 in front of the complement. Now it's 100, you can change I to 100000.

for (var i = 0 ; i <= 100; i ++){  var zero = "";  for (var j = 7-i.toString().length; j > 0; j--) {    zero += "0";  }  console.log(zero + i);}

is actually a zero-sum problem, Python 3 writes this:

["{:0>7}".format(i) for i in range(1, 100001)]

And then provide a JS to the wording

Array.from(Array(1000000).keys()).map(function(x){ return "0".repeat(8 - ("" + (x + 1)).length) + (x+1)})

Php

for ($i=0;$i<=9999999;$i++) echo str_pad($i,7,"0",STR_PAD_LEFT);

Provide a method that may be available in the future.

for (let i = 0; i <= 100; i++) console.log(String(i).padStart(7,'0'))

function incrace(){    console.log((n=>(7-n.length)>0?(new Array(7-n.length+1)).join(0)+n:n)(String(i++)));    setTimeout(incrace,500);}var i = 0;incrace();

After writing to find that the problem has been answered ... JS code, for reference

Js

var len = 10;    //长度for (var i = 1; i <= 100; i++) {    console.log((Array(len).join(0) + Math.abs(i)).slice(-len));}

Come and come ... On a set of PHP version of the wild path

// 定义一个位数比结果位数多的初始值$base_num = 10000000;// 开搞for ($i = 0; $i < 100; $i++) {    echo substr($base_num += $i, -7), "\n";}

Look at my C-language:

for (int i = 0; i < 10000000; i++) {    printf("%07d\n", i);}

The length after 0 is actually also available:

// 补零后长度, 注意printf的变化~in len = 7;for (int i = 0; i < 10000000; i++) {    printf("%0*d\n", len, i);}

I find that many of the answers are not real algorithms, and I thought the implementation of the C-language printf function would be similar.

The previous C-language implementation of the printf function, this problem does not have to calculate the length

Please run on a compiler that supports C99. (C89 not supported)

 #include  #include  void Ltoa (long num, int width, char *str) {static char digs[] = "0123456789";//To facilitate extended hex char ac[width+1];//C99 support; +1 is because the string must end with '% ', but this does not count toward the string length. int i = width; This variable can be used directly instead of I, but this function is I wrote before, too lazy to change the following code memset (&ac, ' 0 ', width); Directly put all memory first ' 0 ' ac[i] = ' + ';//The end of the string must be ' \ n '//followed by an integer-to-string code, the idea is that the division and the remainder to get each digit (the conversion is the same idea)//For example 123/10 = 12_3 12/10 = 1_2//1/10 = 0_1//You can see that the remainder of the 3,2,1 is inverted. Copy the 3, 2, 1 upside-down to the memory space. if (num) ac[--i] = digs[num% 10]; num = NUM/10; while (0 < num && 0 < i) {ldiv_t QR = ldiv (num, 10); ldiv_t with structural body, the main convenience is to remove the remainder and quotient num = Qr.quot; Ac[--i] = Digs[qr.rem]; Converting 0,1,3,4 to ' 0 ', ' 1 ', ' 2 ', ' 3 ', can also be used 0+ ' 0 ' method, but it has been explained above, in order to extend the convenience of other systems. } int n = sizeof (AC)-I; memcpy (str, &AC, width + 1);//Copy the string and the \ n to the buffer.}  

    int max = 一个数字;    for (int i = 0; i < max; i++) {        String tem = "";        for (int j = 0; j < len - (i + "").toString().length(); j++) {            tem = tem + "0";        }        System.out.println(tem+i);    }

Somebody change the value to more than 100000?

Why is there no C + + version AH.
I'm relieved to see how complicated you're writing.

Is there any algorithm to write? To a MySQL version of:

CREATE TABLE `test` (  `tid` int(7) UNSIGNED ZEROFILL DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.