免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 2953 | 回复: 25
打印 上一主题 下一主题

求第二问解法,晕了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-07 10:04 |只看该作者 |倒序浏览
A C language function takes 3 positive integers (A, B, C) C is equal
to either A or B, return the other value without using any comparisons
or test expressions (if, while, ? : ), what if there are 4 positive
integers (A, B, D, C) and C is equal to either A, B, or D, return one of
the other two values (same constraints)

论坛徽章:
0
2 [报告]
发表于 2006-09-07 10:14 |只看该作者
c 异或 a or b , then 或 a or b , return the value

论坛徽章:
0
3 [报告]
发表于 2006-09-07 10:20 |只看该作者
感觉三个参数的那个函数可以写成这样:

   int fun(int a,int b,int c)
             {
                return a+b-c;
               }

论坛徽章:
0
4 [报告]
发表于 2006-09-07 10:31 |只看该作者
应该是这样吧:

原帖由 kf701 于 2006-9-7 10:14 发表
c 异或 a or b , then 或 a or b , return the value


两次异或就可以还原

论坛徽章:
0
5 [报告]
发表于 2006-09-07 10:50 |只看该作者
原帖由 nhjidle 于 2006-9-7 10:04 发表
A C language function takes 3 positive integers (A, B, C) C is equal
to either A or B, return the other value without using any comparisons
or test expressions (if, while, ? : ), what if there  ...


int fun(int a, int b, int c)
{
    return a + b + ~c + 1;
}

论坛徽章:
0
6 [报告]
发表于 2006-09-07 10:53 |只看该作者
A C language function takes 3 positive integers (A, B, C) C is equal
to either A or B, return the other value without using any comparisons
or test expressions (if, while, ? : ), what if there are 4 positive
integers (A, B, D, C) and C is equal to either A, B, or D, return one of
the other two values (same constraints)

我想LZ的意思主要是4个参数的吧?

题目不就是第二问解法吗?

论坛徽章:
0
7 [报告]
发表于 2006-09-07 11:51 |只看该作者
原帖由 lonelyair 于 2006-9-7 10:53 发表

我想LZ的意思主要是4个参数的吧?

题目不就是第二问解法吗?


哦,看错了。

int fun3(int a, int b, int c)
{
    return a + b - c;
}

int fun4(int a, int b, int c, int d)
{
     //如果a == c则返回,b,否则返回a,没d什么事。
    return !(a - c) * b + !!(a - c) * a;  
}

论坛徽章:
0
8 [报告]
发表于 2006-09-07 12:10 |只看该作者
原帖由 ftInternet 于 2006-9-7 11:51 发表


int fun4(int a, int b, int c, int d)
{
     //如果a == c则返回,b,否则返回a,没d什么事。
    return !(a - c) * b + !!(a - c) * a;  
}


如果 b==a 呢?

应该调用 fun3

论坛徽章:
0
9 [报告]
发表于 2006-09-07 12:18 |只看该作者
原帖由 win_hate 于 2006-9-7 12:10 发表


如果 b==a 呢?

应该调用 fun3


按题目来说,应该不会同时两个与C相等的吧。不然3个都与C相等,那就。。。

论坛徽章:
0
10 [报告]
发表于 2006-09-07 12:36 |只看该作者
应该是返回除与c相等的另外两个数的值


a==b
return (c &d)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP