Javascript-an algorithm? 0000000 sequence increments

Source: Internet
Author: User
For example, 0000000 increments. the format is as follows: {code...} imitates @ G_Koala_C's answer and writes the php version: {code ...}

For example, 0000000 increments. the format is as follows:

000000100000020000003 ...... increment to 100000010 ...... increment to 1000000100 ...... increment to 1000000100000 ...... increment

Imitating @ G_Koala_C's answer, I 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:

For example, 0000000 increments. the format is as follows:

000000100000020000003 ...... increment to 100000010 ...... increment to 1000000100 ...... increment to 1000000100000 ...... increment

Imitating @ G_Koala_C's answer, I 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.'
';}

In js writing, the principle is to calculate the length of the number and then add zero at the front. 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);}

Actually, it's the amount of zero-padding issues. Python 3 writes this:

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

Provide a js writing method

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);

Provides 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();

I found the problem answered after writing... Js code for reference

JS

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

Come and come... a PHP wild path

// Define the initial value of a single digit that is more than the result digit $ base_num = 10000000; // activate for ($ I = 0; $ I <100; $ I ++) {echo substr ($ base_num + = $ I,-7), "\ n ";}

In my C language:

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

The length after zero padding is actually configurable:

// The length after zero padding. pay attention to the changes in printf ~ In len = 7; for (int I = 0; I <10000000; I ++) {printf ("% 0 * d \ n", len, I );}

I found that many of the answers are not true algorithms. I thought it was similar to the implementation of C-language printf functions.

The last C language used to simulate the printf function does not require length calculation.

Run on the compiler that supports C99 (not supported by C89)

# Include
  
   
# Include
   
    
Void ltoa (long num, int width, char * str) {static char digs [] = "0123456789"; // to facilitate expansion of hexadecimal char ac [width + 1]; // supported by C99; + 1 is because the string must end with '\ 0', but this is not included in the string length. int I = width; // This variable can be directly replaced by I, but this function was previously written by me and I am too lazy to change the following code memset (& ac, '0 ', width); // directly set all the memory to '0' ac [I] = '\ 0 '; // the end of the string must be '\ 0' // The code followed by an integer to a string. The idea is to get each digit by division and remainder (the same idea is true for hexadecimal conversion) // for example, 123/10 = 12_3 // 12/10 = 1_2 // 1/10 = 0_1 // you can see that the remainder 3, 2, and 1 are put upside down to, 3. // Copy the values 3, 2, and 1 into the memory. if (num) ac [-- I] = digs [num % 10]; num = num/10; while (0 <num & 0 <I) {lp_t qr = lp (num, 10); // The lp_t struct is used to conveniently retrieve the remainder and num = qr at a time. quot; ac [-- I] = digs [qr. rem]; // converts, to '0', '1', '2', '3', or 0 + '0, but it has already been explained above, in order to extend to other hexadecimal convenience .} int n = sizeof (ac)-I; memcpy (str, & ac, width + 1); // Copy the string and \ 0 to the buffer .}
   
  

Int max = a number; 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 );}

Can someone change the value to 100000 or above?

Why is there no C/C ++ version.
I can rest assured that what you wrote is so complicated.

What algorithm does this need to write? Mysql version:

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.