挑战 f(f(n)) = -n

wuyi8808 2009-05-26 03:11:21
加精

源起:http://topic.csdn.net/u/20090525/17/4d0cb4ed-b503-42b6-b7d3-3528b3aaebf7.html


设计一个函数 f,使得

f(f(n)) = -n

规定 f(n) 的定义域和值域都是 8 比特的有符号整数,其范围:-128 ~ 127。
显然,n = -128 时无法满足要求,因为其相反数 128 已经超出范围。
现在请问,能否做到对其余的 n 都满足上式。


也就是说,想办法使以下程序的输出为空:
(注意,您只能在 // TODO 处写语句,不能改动程序的其余任何部分)

using System;
using tiny = System.SByte;

class FFN
{
static void Main()
{
for (tiny ffn, n = tiny.MaxValue; n > tiny.MinValue; n--)
if ((long)(ffn = f(f(n))) != -(long)n)
Console.WriteLine("n={0} ffn={1} ERROR", n, ffn);
}

static tiny f(tiny n)
{
// TODO: 在这里发挥您的聪明才智。
}
}
...全文
1344 109 打赏 收藏 转发到动态 举报
写回复
用AI写文章
109 条回复
切换为时间正序
请发表友善的回复…
发表回复
ylwqhr 2009-12-28
  • 打赏
  • 举报
回复
收藏,学习!~
mxc1225 2009-11-26
  • 打赏
  • 举报
回复
厉害!
beiguoyouzi 2009-07-23
  • 打赏
  • 举报
回复
mark,以后再看。
wuyi8808 2009-07-03
  • 打赏
  • 举报
回复
[Quote=引用 84 楼 vwxyzh 的回复:]
我在54楼帖的代码被无视了?
[/Quote]

54楼的代码在 Debug 模式下是正确的,编译为 Relase 模式就不对了,或用命令行编译器 csc.exe 编译也不对。另,用 Linux mono 编译器也不对。
zhangxun2007 2009-06-18
  • 打赏
  • 举报
回复
Paradin 2009-05-30
  • 打赏
  • 举报
回复
mark
xudongdong_1990 2009-05-28
  • 打赏
  • 举报
回复
感谢楼主的这个帖子,学习了不少,帮顶!
xudongdong1990 2009-05-28
  • 打赏
  • 举报
回复
学习了,帮顶!
aimeast 2009-05-28
  • 打赏
  • 举报
回复
经过多方面的思考,不借助其它的临时变量,是没有办法实现的
Dhoopu 2009-05-28
  • 打赏
  • 举报
回复
学习了^
innerlee 2009-05-28
  • 打赏
  • 举报
回复

static tiny f(tiny n)
{
Random r = new Random();
return (tiny)r.Next(-127, 127);
}

从数学上是可以给出无ERROR解的,
从微软Random类的实现看是不可能的.
innerlee 2009-05-28
  • 打赏
  • 举报
回复

static tiny f(tiny n)
{
if (Console.Title.EndsWith(".")) {
Console.Title = Console.Title.Substring(0, Console.Title.Length - 1);
return (tiny)(-n);
} else {
Console.Title += ".";
return n;
}
}


这是实打实的解决方案,
虽然有些虚.
innerlee 2009-05-28
  • 打赏
  • 举报
回复

static tiny f(tiny n)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter sw1 = new System.IO.StreamWriter(ms);
Console.SetOut(sw1);
ms.Close();
return new tiny();
}


从输出上是正确的,
从情感上是不可接受的.
iloveppmm 2009-05-28
  • 打赏
  • 举报
回复
[Quote=引用 95 楼 jesunmy2008 的回复:]
楼上的,你少了一个条件f(-f(n))=n
[/Quote]

????????

f(n)=-n

-f(n)=n;

f(-f(n))=f(n)=n;
你确定有这个条件吗??

那除了n=0,还有解吗??
jesunmy2008 2009-05-28
  • 打赏
  • 举报
回复
楼上的,你少了一个条件f(-f(n))=n
iloveppmm 2009-05-28
  • 打赏
  • 举报
回复
f(n)=m

f(m)=-n;

求解f(x)

f(-f(n))=n;
f(f(n))=-n;

f(x)=-f(-x);

f(-x)=-f(x);//这个函数很常见吧?? 高中时经常见到。

f(x)=-x;

f(-x)=-(-x)=-f(x);

所以: 函数为f(x)=-x; 在x为实数可满足条件。

这是纯数学的。

计算机的 不会。 总觉得有符号 溢出之类的挺麻烦。

「已注销」 2009-05-27
  • 打赏
  • 举报
回复
我算算看。
sawam 2009-05-27
  • 打赏
  • 举报
回复
上面的我贴的代码有问题.
正确的

int f(int n)
{

int result = n;
if (result < 0)
{
result = Math.Abs(result); //取绝对值
result = result | 0x60000000;
}
else if ((result & 0x40000000) == 0)
{
result = n | 0x40000000;
}
else
{
if ((result & 0x20000000) == 0)
{
result = (int)((long)result & 0xBFFFFFFF);
result = result * -1;
}
else
{
result = (int)(((long)result) & 0x9FFFFFFF);
}
}
return result;
}
sawam 2009-05-27
  • 打赏
  • 举报
回复
题目要求其中有1条,如果你无法设计出一个函数使得其对32比特下的所有整数都适用,那么设计此函数使得其能够适用于尽可能多的整数。

我的算法,取值范围比int类型小2倍,算法原理: 最高位和最高位的下一位当标志位,
C#的代码如下

 
int f(int n)
{

int result = n;
if (result < 0)
{
result = Math.Abs(result); //取绝对值
result = (int)((long)result | 0xC0000000);
}
else if ((result & 0x80000000) == 0)
{
result = (int)((long)result | 0x80000000);
}
else
{
if ((result & 0x40000000) == 0)
{
result = (int)((long)result & 0x7FFFFFFF);
result = result * -1;
}
else
{
result = (int)(((long)result) & 0x3FFFFFFF);
}
}
return result;
}

xudongdong_1990 2009-05-27
  • 打赏
  • 举报
回复
楼主干脆把答案告诉大家好了,呵呵!
加载更多回复(87)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧