嗨这个功能是做什么的? [英] Hi what this function do?

查看:143
本文介绍了嗨这个功能是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,有人可以解释这个功能在做什么吗?特别是最后一部分。





  int  fu2( int  * a, int  b); 
int main()
{
int x = 38953 ;
int arr [] = { 4 , - 5, 2 , - 3, 4 , - 4, 6 };

printf( %d \ n,fu2(arr, 7 ));
return 0 ;
}
int fu2( int * a, int b)
{
int c = * a;
if (b> 1)
c = fu2(a + 1,b-1);
return (* a< 0?(* a> c?* a:(c< 0?c:* a)):( c< 0 ?c: 0 ));
}

解决方案

它看起来像一个冒泡排序,也许是一个自定义排序。相当可怕的代码严重缺乏评论,很可能是由一个试图聪明的人写的。这发生就好了。

有两点需要注意:

fu2 是递归的,即它是一个自我调用的函数。 />
第三级运营商?是这样的。

x? y:z; 表示

 if(x)
{
y;
}
其他
{
z;
}





所以fu2可以改写为

 int fu2 (int * a,int b)
{
int c = * a;
if(b> 1)//结束条件,当b达到1
时停止递归{
c = fu2(a + 1,b - 1); //递归调用fu2函数
}

if(* a< 0)
{
if(* a> c)
{
return * a ;
}
else
{
if(c< 0)
{
return c;
}
其他
{
返回a;
}
}
}
其他
{
如果(c <0?)
{
返回c;
}
其他
{
返回0;
}
}
}





现在你可以关注它正在做的事情,插上在数字中并通过它在dugger中逐步看看会发生什么。密切观察阵列的内容但是进行了扩展它看起来不会变得如此,所以这可能是对最小负数的某种搜索。

告诉我们什么时候你破解了它。


Hey guys, can someone please explain what this function is doing? Especially the last part.


int fu2(int *a, int b);
int main()
{
  	int x = 38953;
	int arr[]={4,-5,2,-3,4,-4,6};
	
	printf("%d\n",fu2(arr,7));
	return 0;
}
int fu2(int *a, int b)
{
    int c=*a;
    if(b>1)
        c=fu2(a+1,b-1);
    return (*a<0 ? (*a>c ? *a :(c<0?c:*a)) : (c<0?c:0));
}

解决方案

It looks something like a bubble sort perhaps a custom one. Fairly horrible code that is seriously lacking comments and may well have been written by someone trying to be clever. That happens enough.
Two things to note:
fu2 is recursive, i.e. it''s a function that calls itself.
The tertiary operator ? works like this.
x ? y : z; means

if( x )
{ 
  y; 
}
else
{
  z;
}



so fu2 can be rewritten as

int fu2(int *a, int b)
{
    int c=*a;
    if( b > 1 )//end condition, stop recursing when b reaches 1
    {
        c = fu2( a + 1, b - 1 );//recursive call to fu2 function 
    }
    
    if( *a < 0 )
    {
       if( *a > c )
       {
         return *a;
       }
       else
       {
          if( c < 0 )
          {
             return c;
          }
          else
          {
             return a;
          } 
       }
    }
    else
    {
       if( c < 0 ? )
       {
         return c;
       }
       else
       {
         return 0;
       }
    }
}



Now you can follow what it''s doing, plug in the numbers and step through it in the dugger to see what happens. Keep a watch on the contents of the array but having done the expansion it doesn''t look like it gets alterred so this is some sort of search possibly for the smallest negative number.
Let us know when you''ve cracked it.


这篇关于嗨这个功能是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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