[C + + artifice] No loop to determine output 5 to 1

Source: Internet
Author: User

Occasionally see a problem, and buddies a few discussion, this thing, not the loop is recursive, of course, as long as the goal, tube him what loop recursion, right. Now to summarize all the methods we can think of, we have a new Idea welcome thread discussion ~ ~

OK, start.

The first one, the simplest, is the direct output. (because the output is almost the same, it's not one.)

printf ("5 4 3 2 1\n");

The second, the simple loop.

For loop for (int =5; I!=0; i--) {printf ("%d\n", I);} While loop int i=6;  while (--) {printf ("%d\n", I);} Do While loop int i=5; do{Prinf ("%d\n", I);} while (-a);

Three, recursion. Now the basic cycle is finished, to discuss recursion, first a simple point.

#include <stdio.h>void print (int i) {if (i) {printf ("%d\n", I);    Print (-i);    }}int Main () {print (5); return 0;}

Then another buddy Lol, came a short-circuit recursion.

#include <stdio.h>int print (int n) {printf ("%d\n", N); --n && print (n);}    int main () {print (5); return 0;}

Four, C + + play. In the group there is a friend in C + + is not satisfied, but also to a class of construction.

C + + #include <iostream>class a{public:static int n; A () {std::cout<<n--<<std::endl;}};    int A::n=5;int Main () {new a[5]; return 0;}

Moderator said you scum, look at the elder brother to you play the template.

#include <iostream>using namespace std;template <int t>class f:public f<t+1> {public:f () {cout <&L T T << "";}};        Template <>class f<6> {};int main () {f<1> fu;        cout<<endl; return 0;}

Five, the new return of recursion. See everybody so lively, I also want to write one, but C + + is not skilled, suddenly think of a feature of GCC, main can also recursive AH

GCC through, vs No, clang report two warnings include <stdio.h> #include <stdlib.h>void main (int i) {printf ("%d\n", 6-i); (&main + (&exit-&main) * (I/5)) (i+1);}

Six, assembly + open hanging. Then a compilation Daniel talk, you bb What, elder brother wrote code you can not understand, and then foreign flavor to throw us a.

This failed to compile under GCC # include "stdio.h" int main () {__asm{push ESI mov esi,0x31buhaha:push ESI call Putchar push 0x0a cal L Putchar Add esp,8 inc ESI CMP esi,0x36 jne buhaha pop esi} return 0;}

We all agreed to say that this is not count, the big God impatient and fill in two lines of code

There's nothing to say.

You think it's over, of course not,

We have not yet reacted, the great God spoke again, elder brother without Assembly also can open hang, hehe.

Successful compilation under GCC, running a segment error # include <stdio.h>typedef void (*hehe); int main () {static char a[]={0x56, 0x57, 0xBF, 0x78, 0x5 6, 0x34, 0x12, 0xBE, 0x31, 0x00,0x00, 0x00, 0x56, 0xFF, 0xD7, 0x6A, 0x0A, 0xFF, 0xD7, 0x83,0xc4, 0x08, 0x46, 0x83, 0xFE, 0    x36, 0x75, 0xF0, 0x5f, 0x5E, 0xC3};    * (Unsigned long *) (&a[3]) = (unsigned long) Putchar; ((hehe) (&a[0])) ();}

Everybody's not talking.


Summary: Said so many of the C + + method, other languages must also have a more "abnormal" method, usually encountered a problem when we may be a person of limited thinking, but with the people to exchange, there is always a variety of solutions, so, do not skimp on your problem solving methods, take out everyone exchange it:)

[C + + artifice] No loop to determine output 5 to 1

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.