如何回归真假的对立面?

时间:2016-02-02 06:37:09

标签: c algorithm data-structures

我在公司接受采访时被问到一个问题,问题有点奇怪,所以想和专家一起询问。

问题假设我有返回bool类型的函数。我们这样说:

public bool func(int param)
{
  bool retVal;

 // here is some algorithm which as a result either set the retVal to false or true, 
 // It doesn't matter what is algo, the only thing important is it either do 
 // retVal=false or retVal=true

 // The question is i have to write the algo here which in case 
 // if the previous algo gives us retVal=false then it should 
 // return true and if retVal=true then return false      

}

算法应该是什么?

3 个答案:

答案 0 :(得分:0)

我认为你的代码是这样的:

public bool func(int param)
{
   bool retVal;
   return !retVal;
}

答案 1 :(得分:0)

!retVal与retVal相反。如果retVal为true,则!retval为false,反之亦然

public bool func(int param)
{
 bool retVal
 //your algo;
 return !retVal;

}

答案 2 :(得分:-1)

如果算法的结果是retval,

return (!retval);