任务:打印从1到1000的数字,不使用任何循环或条件语句。不要只写printf()或cout语句1000次。
用C或c++怎么做呢?
任务:打印从1到1000的数字,不使用任何循环或条件语句。不要只写printf()或cout语句1000次。
用C或c++怎么做呢?
当前回答
也可以通过简单的动态调度来实现(在Java中也适用):
#include<iostream>
using namespace std;
class U {
public:
virtual U* a(U* x) = 0;
virtual void p(int i) = 0;
static U* t(U* x) { return x->a(x->a(x->a(x))); }
};
class S : public U {
public:
U* h;
S(U* h) : h(h) {}
virtual U* a(U* x) { return new S(new S(new S(h->a(x)))); }
virtual void p(int i) { cout << i << endl; h->p(i+1); }
};
class Z : public U {
public:
virtual U* a(U* x) { return x; }
virtual void p(int i) {}
};
int main(int argc, char** argv) {
U::t(U::t(U::t(new S(new Z()))))->p(1);
}
其他回答
既然对bug没有限制…
int i=1; int main() { int j=i/(i-1001); printf("%d\n", i++); main(); }
或者更好(?),
#include <stdlib.h>
#include <signal.h>
int i=1;
int foo() { int j=i/(i-1001); printf("%d\n", i++); foo(); }
int main()
{
signal(SIGFPE, exit);
foo();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef void(*word)(int);
word words[1024];
void print(int i) {
printf("%d\n", i);
words[i+1](i+1);
}
void bye(int i) {
exit(0);
}
int main(int argc, char *argv[]) {
words[0] = print;
words[1] = print;
memcpy(&words[2], &words[0], sizeof(word) * 2); // 0-3
memcpy(&words[4], &words[0], sizeof(word) * 4); // 0-7
memcpy(&words[8], &words[0], sizeof(word) * 8); // 0-15
memcpy(&words[16], &words[0], sizeof(word) * 16); // 0-31
memcpy(&words[32], &words[0], sizeof(word) * 32); // 0-63
memcpy(&words[64], &words[0], sizeof(word) * 64); // 0-127
memcpy(&words[128], &words[0], sizeof(word) * 128); // 0-255
memcpy(&words[256], &words[0], sizeof(word) * 256); // 0-511
memcpy(&words[512], &words[0], sizeof(word) * 512); // 0-1023
words[1001] = bye;
words[1](1);
}
有趣的函数指针(不需要新流行的TMP):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MSB(typ) ((sizeof(typ) * CHAR_BIT) - 1)
void done(int x, int y);
void display(int x, int y);
void (*funcs[])(int,int) = {
done,
display
};
void done(int x, int y)
{
exit(0);
}
void display(int x, int limit)
{
printf( "%d\n", x);
funcs[(((unsigned int)(x-limit)) >> MSB(int)) & 1](x+1, limit);
}
int main()
{
display(1, 1000);
return 0;
}
附注:我将禁止条件符扩展到逻辑运算符和关系运算符。如果允许逻辑否定,递归调用可以简化为:
funcs[!!(limit-1)](x+1, limit-1);
没人说它不应该事后分段错误,对吧?
注意:这在我的64位Mac OS X系统上正常工作。对于其他系统,您需要将参数更改为setrlimit,并相应地更改spacecew的大小。: -)
(我不应该包括这个,但以防万一:这显然不是一个好的编程实践的例子。然而,它确实有一个优点,那就是它利用了这个网站的名称。)
#include <sys/resource.h>
#include <stdio.h>
void recurse(int n)
{
printf("%d\n", n);
recurse(n + 1);
}
int main()
{
struct rlimit rlp;
char spacechew[4200];
getrlimit(RLIMIT_STACK, &rlp);
rlp.rlim_cur = rlp.rlim_max = 40960;
setrlimit(RLIMIT_STACK, &rlp);
recurse(1);
return 0; /* optimistically */
}
编译时递归!: P
#include <iostream>
template<int N>
struct NumberGeneration{
static void out(std::ostream& os)
{
NumberGeneration<N-1>::out(os);
os << N << std::endl;
}
};
template<>
struct NumberGeneration<1>{
static void out(std::ostream& os)
{
os << 1 << std::endl;
}
};
int main(){
NumberGeneration<1000>::out(std::cout);
}