One of the eclectic editing proceduresCyclic printing AlgorithmAuthor: Zhu YunxiangIn the novels, some people often say that the generals of XX are arrogant, so that the enemy cannot be prevented. For example, Li Mu in "Qin Kee", for example, Xiao Shao in "Great Tang and Double Dragon Chuan. What kind of thinking should be made during programming? It cannot be bound by the rules. For example: Example 1: compile a function void fun (int t ), it is required to print t "*" numbers on the screen, where t length is not greater than 50. Design 1 ]:650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> void fun (int t) 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> { 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> if (t <0 | t> 50) return; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> switch (t) 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> { 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> case 1: printf ("*"); break; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> case 2: printf ("**"); break; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> delete 500 words here 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> case n: printf ("****** ...... **"); break; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/>} 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/>} Although the above method is clumsy, the time complexity is O (1), fast! This is to sacrifice space for time. Of course, there are usually not many people writing Design 1, and most people use design 2.Design 2 ]:650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> void fun (int t) 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> { 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> int I; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> if (t <0 | t> 50) return; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> for (I = 0; I <t; I ++) printf ("*"); 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/>} The above algorithms are the most authentic algorithms! Advantage: The program is quite simple and the idea is quite clear. The disadvantage is that the time complexity is O (t ).Design 3 ]:650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> void fun (int t) 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> { 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "/> char * p = "******************************** **************************************** ****************"; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> char buf [51]; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> if (t <0 | t> 50) return; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> memcpy (buf, p, t ); 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> buf [t] = 0; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> printf (buf ); 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z644AB-0.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/>} at first glance, it is really troublesome, but time complexity O (1 ). Some people say that the previous Code such as memcpy will not take time? Answer: 1. the time spent on memecpy is far from that of screen display functions such as printf. 2. Even after the for loop of the empty loop body is compiled, several lines are generated, which is slower than memcpy. However, the storage space of p and buf is added here.Design 4]: 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> void fun (int t) 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> { 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> char * p; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> if (t <0 | t> 50) return; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> if (! (P = (char *) malloc (t + 1) return; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> memset (p, '*', t ); 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> p [t] = 0; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> printf (p ); 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> free (p ); 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)"/> return; 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1Z6443100-26.gif "align =" top ". click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "/>} is similar to design 3, but it reduces unnecessary space waste, but increases the malloc memory allocation and memset memory settings, it increases the time consumption. Time complexity O (1 ). Writing software is not an eclectic one. It doesn't mean that you want to write any program to be arrogant. Instead, you can try other ways when you are on a normal road or when it is very difficult to go. Just like the above algorithms, each has its own advantages and disadvantages and has its own applicability. Do not use only one method, nor use unsuitable algorithms for the sake of high brightness. |
Related Articles: One of the eclectic editing programs the loop printing algorithm is the second of the Code. The new row algorithm in the Russian square is the third of the program. The variable value exchange algorithm |
Various forms of a four-cycle program
This article from the "programming prodigal son Zhu Yunxiang" blog, please be sure to keep this source http://zhuyunxiang.blog.51cto.com/653596/128441