Print the number from 1 to 1000, but you cannot use any of the loop statements or conditional statements.

Source: Internet
Author: User

Look at a very interesting topic.

The topic is this:

Please print the number from 1 to 1000, but you cannot use any of the loop statements or conditional statements.

I was also blindfolded when I saw the problem.

The first thing to think about is the recursive implementation,

But how do you jump out of this loop?


I thought about it, someone gave a hint that can be used with to judge:

Look at the code first:

#include <stdio.h>int print (int num) {num && print (num-1);p rintf ("%d", num); return num;} int main () {int num = 1;print (+); return 0;}

When you see this code, the person you know about C should know what that means.

In the C language, the,&& of this logical condition is preceded by 1, after continuing to judge this condition.

So it's easy to look at it, put a recursive call in the second with the condition, and then use the rules to make it simple to call

There's actually an algorithm, but I'm not able to use it when I debug in VS2010:


This algorithm means that the use of the divisor in the program can not be 0 to jump out of the loop, the result is output, so this method is not possible.

Then there is another way to be interesting.

In fact, in this method of the first idea, very interesting people think I will play 1000 printf.

It's a way, but it's too clumsy.


In fact, macros are a powerful feature that you can use with macros.

#include <stdio.h> #define OUT (i) printf ("%d\n", i++), #define NEC (n) n n n n n. N n n n n n//10^n printf#define outt (i) n EC (NEC (Out (i))) int main () {int i = 1;outt (i); return 0;}

This macro replacement is very interesting, a few lines of code, representing the 1000 printf, haha

This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1682921

Print the number from 1 to 1000, but you cannot use any of the loop statements or conditional statements.

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.