编号序列上的C程序,无循环 [英] C program on number sequence without loops

查看:50
本文介绍了编号序列上的C程序,无循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码如何工作?

  int  main()
{
  静态  int  n =  1 ;

  printf(" ,n)&& (n ++< 100)&&主要的();

  返回  0 ;
} 

解决方案

它不使用循环,因为它正在使用递归.并且由于C默认情况下使用短路评估,因此第二种情况将停止递归.它使用静态变量在递归调用之间传递当前迭代.在这种情况下,此后一种方法甚至比使用参数还要好,因为它在堆栈中仅存储一次,因此该参数将被存储多次调用递归.静态变量仅在第一个声明时初始化,不会在每次调用时都重置为一个,因此可以存储第二个条件中嵌入的后缀增量值.在其他解决方案中提到过,但可能对初学者很感兴趣:


  1. 之所以有效,是因为printf(...)返回一个非0值(打印的字符数),并且由于任何不等于0的值在C中都被解释为true.
  2. 整体的唯一目的x && y & z是一个接一个地求值:首先是x,如果x求真,则执行y,如果y求真,则执行z.结果没有存储,因此main()的尾调用返回0 = false无关紧要-它在内部递归之前可以返回0值.



干杯
Andi


由于短路评估 [ ^ ]您的代码与此等效:

  int  n =  1 ; // 如果是非Pod的可初始化"类型,则
         // 与函数内部的声明不相等
         // ,即使我们忽略了可访问性的差异
 int  main()
{
  如果(printf("  ,n))
  {
    如果(n ++<  100 )
    {
      主要的();
    }
  }
 
  返回  0 ;
} 



如果您想了解这一点,佐尔坦(Zoltán)提到的递归也非常重要: http://en.wikipedia.org /wiki/Recursion_%28computer_science%29 [ ^ ].
如果您不知道堆栈是什么,则需要研究的重要内容(我认为): http://en.wikipedia. org/wiki/Call_stack [ ^ ].


how does this code work??

int main()
{
  static int n=1;

  printf("%d ", n) && (n++<100) && main();

  return 0;
}

解决方案

It does not use loop, because it is using recursion. And as C is using short circuit evaluations by default, the second condition will stop the recursion. It is using a static variable to pass the current iteration between the recursive calls. This later approach is even better in this case - than using a parameter -, since it is stored only once in the stack - the parameter would be stored as many times the recursion is called. The static variable is initialized only at the first declaration, will not be reset to one on every call, thus can store the postfix incremented value embedded in the second condition.


Two things that are not mentioned in the other solutions but might be of interest to the beginner:


  1. It works since printf(...) returns a non-0 value (the number of printed characters) and since any value not equal to 0 is interpreted as true in C.
  2. The only purpose of the whole x && y & z is to evaluate one after the other: first x, if x evaluates to true, do y, if y evaluates to true, do z. The result is not stored, and therefore, it does not matter that the tail call of main() returns 0 = false - it does the recursion internally before it can return the 0 value.



Cheers
Andi


Because of the short-circuit evaluation[^] your code is equivalent with this:

int n=1; // in case of non-pod "initializable" types this
         // isn't equivalent with declaration inside a function
         // even if we overlook the difference in accessiblity
int main()
{
  if (printf("%d ", n))
  {
    if (n++ < 100)
    {
      main();
    }
  }
 
  return 0;
}



EDIT: Recursion mentioned by Zoltán is also very important if you want to understand this: http://en.wikipedia.org/wiki/Recursion_%28computer_science%29[^].
An important stuff (in my opinion) to study if you don''t know what the stack is: http://en.wikipedia.org/wiki/Call_stack[^].


这篇关于编号序列上的C程序,无循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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