打印1至1000,使用了循环 [英] Print 1 to 1000 with out using loop

查看:133
本文介绍了打印1至1000,使用了循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在C ++编程方面的问题,我查了解决方案,我的一个朋友给我这个code的作品完美,但我不明白它的逻辑,以及如何它的作品。我问他这件事,但是他也不知道该方案的实际工作原理,我觉得他也借此解决方案从某处。任何人都可以解释这背后我的意思是在该行的逻辑
 (安培;主+ (安培;出口 - &安培;主)*(J / 1000))(J + 1);

i see the question on a c++ programming context, i check for a solution and one of my friend give me this code its works perfect but i can't understand it's logic and also how it's works. i asked to him about it but he also don't know how the program is actually works, i think he is also take this solution from somewhere. Anybody can explain the logic behind this i mean in the line (&main + (&exit - &main)*(j/1000))(j+1); ?

#include <stdio.h>
#include <stdlib.h>

void main(int j) {
  printf("%d\n", j);
  (&main + (&exit - &main)*(j/1000))(j+1);
}

在此先感谢

推荐答案

它的工作原理如下:

执行与 INT 焦耳/ 1000 ,它会返回 0 总是同时Ĵ 1000 。
使指针操作如下:

Performs the int division j/1000, which will return 0 always while j is smaller than 1000. So the pointer operation is as follows:

&main + 0 = &main, for j < 1000.

然后调用传递作为参数指针指向操作所产生的功能 J + 1
Ĵ小于 1000 ,它会调用主递归与参数比前一步一个。

Then it calls the resulting function pointed by the pointer operations passing as parameter j+1. While j is less than 1000, it will call main recursively with parameter one more than the step before.

j值达到 1000 ,那么整数除法焦耳/ 1000 等于 1 ,指针运算结果如下:

When the value of j reaches 1000, then the integer division j/1000 equals to 1, and the pointer operation results in the following:

&main + &exit - &main = &exit.

然后,它调用退出函数,它完成了程序的执行。

It then calls the exit function, which finishes the program execution.

这篇关于打印1至1000,使用了循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆