登录 | 首页 -> 华新鲜事 -> 求学狮城 | 切换到:传统版 / sForum | 树形列表
programming puzzles
<<始页  [1]  末页>> 

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
[吴永铮 (4-24 17:12, Long long ago)] [ 传统版 | sForum ][登录后回复]1楼

interesting... let me try 1&2...1.
#include <stdio.h>
void main() {
if (printf("hello world\n")) {}
}

2.
a=a+b;
b=a-b;
a=a-b;

[hash (4-24 18:10, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼

(引用 hash:interesting... let me try 1&2...1. #include void main() { if (printf("hello world\n")) {} } 2. a=a+b; b=a-b; a=a-b; )世界真奇妙[soc胖浪人 (4-25 0:27, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼

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?
[icky (4-25 11:33, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼

(引用 hash:interesting... let me try 1&2...1. #include void main() { if (printf("hello world\n")) {} } 2. a=a+b; b=a-b; a=a-b; )oh, i was thinking using whilei did not know a pair of empty brackets can follow "if"[icky (4-25 11:34, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼

(引用 icky: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?)i was thinking of checking bit also.no. 4 is tricky...[hash (4-25 11:36, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼

(引用 icky: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 instructionsBy classic (or classical?), I mean a lot of people use the method.[吴永铮 (4-25 12:26, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼

(引用 吴永铮:(3) can be done by less than 5 instructionsBy classic (or classical?), I mean a lot of people use the method.)check equality with all 2's powerthere are at most 32 of them...[icky (4-25 12:56, Long long ago)] [ 传统版 | sForum ][登录后回复]8楼

(引用 icky:check equality with all 2's powerthere are at most 32 of them...)can be simpler and faster[吴永铮 (4-25 15:26, Long long ago)] [ 传统版 | sForum ][登录后回复]9楼

(引用 吴永铮:can be simpler and faster)先模1<<16和除 1<<16两边应该有一边是0,如果两边都不是0,一定就不是2的power了

然后把不为0的那一边再模1<<8和除1<<8

当然这里假设是unsigned int, 32bits
[icky (4-25 16:46, Long long ago)] [ 传统版 | sForum ][登录后回复]10楼

(引用 icky:先模1...)(num > 0) && (((num - 1) & num) == 0) ?[这就是生活 (4-26 0:31, Long long ago)] [ 传统版 | sForum ][登录后回复]11楼

(引用 这就是生活:(num > 0) && (((num - 1) & num) == 0) ?)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.
[吴永铮 (4-26 0:50, Long long ago)] [ 传统版 | sForum ][登录后回复]12楼

(引用 吴永铮:correctjust ((num - 1) & num) is enough ((num - 1) ^ num) also can It's quite practical and quite frequent. It's important to ...)opps ^ is wrong. It's always true.[吴永铮 (4-26 1:06, Long long ago)] [ 传统版 | sForum ][登录后回复]13楼

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;
}
[吴永铮 (4-27 21:56, Long long ago)] [ 传统版 | sForum ][登录后回复]14楼

(引用 吴永铮:my answer to (4)#include int main (void) { const char *str = "#include %cint main (void)%c{%c const char *str = %c%s%c;%...)(Y)[hash (4-29 4:42, Long long ago)] [ 传统版 | sForum ][登录后回复]15楼


<<始页  [1]  末页>> 
登录 | 首页 -> 华新鲜事 -> 求学狮城 | [刷新本页] | 切换到:传统版 / sForum