programming puzzles
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 6 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:吴永铮 (等级:8 - 融会贯通,发帖:2078) 发表:2007-04-24 17:12:48  楼主  关注此帖评分:
programming puzzles
Write a "Hello World" program in 'C' without using a semicolon.
(this is tricky)

C/C++ : Exchange two numbers without using a temporary variable.
(somehow classic way)

C/C++ : Find if the given number is a power of 2.
(somehow classic way)

Write a program whose printed output is an exact copy of the source. Needless to say, merely echoing the actual source file is not allowed.
(a bit hard to think)

source: http://developers.slashdot.org/article.pl?sid=04/12/04/0116231
Put your OWN COOL signature here!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:吴永铮 (等级:8 - 融会贯通,发帖:2078) 发表:2007-04-25 12:26:14  2楼
what do you mean by classic way?C/C++ : Find if the given number is a power of 2. (somehow classic way) can i check bit by bit?
(3) can be done by less than 5 instructions
By classic (or classical?), I mean a lot of people use the method.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:吴永铮 (等级:8 - 融会贯通,发帖:2078) 发表:2007-04-25 15:26:25  3楼
check equality with all 2's powerthere are at most 32 of them...
can be simpler and faster
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:吴永铮 (等级:8 - 融会贯通,发帖:2078) 发表:2007-04-26 00:50:18  4楼
(num > 0) && (((num - 1) & num) == 0) ?
correct
just ((num - 1) & num) is enough

((num - 1) ^ num) also can

It's quite practical and quite frequent. It's important to know it, otherwise you don't understand what's going on.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:吴永铮 (等级:8 - 融会贯通,发帖:2078) 发表:2007-04-26 01:06:43  5楼
correctjust ((num - 1) & num) is enough ((num - 1) ^ num) also can It's quite practical and quite frequent. It's important to know it, otherwise you don't understand what's going on.
opps ^ is wrong. It's always true.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:吴永铮 (等级:8 - 融会贯通,发帖:2078) 发表:2007-04-27 21:56:17  6楼 评分:
programming puzzlesWrite a "Hello World" program in 'C' without using a semicolon. (this is tricky) C/C++ : Exchange two numbers without using a temporary variable. (somehow classic way) C/C++ : Find if the given number is a power of 2. (somehow classic way) Write a program whose printed output is an exact copy of the source. Needless to say, merely echoing the actual source file is not allowed. (a bit hard to think) source: http://developers.slashdot.org/article.pl?sid=04/12/04/0116231
my answer to (4)
#include <stdio.h>
int main (void)
{
const char *str = "#include <stdio.h>%cint main (void)%c{%c const char *str = %c%s%c;%c printf(str, 10, 10, 10, 34, str, 34, 10, 10, 10, 10);%c return 0;%c}%c";
printf(str, 10, 10, 10, 34, str, 34, 10, 10, 10, 10);
return 0;
}
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 6 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码