不用循环和条件判断打印1-1000
来源:互联网 发布:非涉密网络定义 编辑:程序博客网 时间:2024/05/15 05:38
//z 不用循环和条件判断打印1-1000
//z 2011-05-24 19:16:07@is2120
#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);
}
/*
————————————————————————————————————————————*/
/*
@PP, that's quite lengthy to explain, but basically, j is initially 1 because
it's actually argc, which is 1 if the program is called without arguments. Then,
j/1000 is 0 until j becomes 1000, after which it's 1. (exit - main) is, of
course, the difference between the addresses of exit() and main(). That means
(main + (exit - main)*(j/1000)) is main() until j becomes 1000, after which it
becomes exit(). The end result is that main() is called when the program starts,
then calls itself recursively 999 times while incrementing j, then calls exit().
Whew :)
*/
#include <stdio.h>
#include <stdlib.h>
void main(int j) {
printf("%d/n", j);
(&main + (&exit - &main)*(j/1000))(j+1);
}
#include <stdio.h>
#include <stdlib.h>
void f(int j)
{
static void (*const ft[2])(int) = { f, exit };
printf("%d/n", j);
ft[j/1000](j + 1);
}
int main(int argc, char *argv[])
{
f(1);
}
/*————————————————————————————————————————————*/
/*
I'm surprised nobody seems to have posted this -- I thought it was the most
obvious way. 1000 = 5*5*5*8.
*/
#include <stdio.h>
int i = 0;
p() { printf("%d/n", ++i); }
a() { p();p();p();p();p(); }
b() { a();a();a();a();a(); }
c() { b();b();b();b();b(); }
main() { c();c();c();c();c();c();c();c(); return 0; }
/*————————————————————————————————————————————*/
[ Edit: (1) and (4) can be used for compile time constants only, (2) and (3) can
be used for runtime expressions too — end edit. ]
// compile time recursion
template<int N> void f1()
{
f1<N-1>();
cout << N << '/n';
}
template<> void f1<1>()
{
cout << 1 << '/n';
}
// short circuiting (not a conditional statement)
void f2(int N)
{
N && (f2(N-1), cout << N << '/n');
}
// constructors!
struct A {
A() {
static int N = 1;
cout << N++ << '/n';
}
};
int main()
{
f1<1000>();
f2(1000);
delete[] new A[1000]; // (3)
A data[1000]; // (4) added by Martin York
}
/*————————————————————————————————————————————*/
#include <stdio.h>
#define MAX 1000
int boom;
int foo(n) {
boom = 1 / (MAX-n+1);
printf("%d/n", n);
foo(n+1);
}
int main() {
foo(1);
}
//z 2011-05-24 19:16:11@is2120
- 不用循环和条件判断打印1-1000
- 不用循环和条件判断打印1-1000
- 不用条件和循环语句打印出1~1000
- 不用判断和循环打印1000次
- 不用循环\不用条件判断..输出1 - 1000
- 不用循环打印 1-1000
- 合并链表和求1+2+...+n不用循环、乘除法、循环、条件判断、选择相关的关键字
- 条件判断和循环
- 条件判断和循环
- 条件判断和循环
- 条件判断和循环
- Java实现1+2+...+n,不用乘除符号,不用if等条件判断,不用for等循环
- Python_条件判断和循环
- python10:条件判断和循环
- shell循环和条件判断
- 3.条件判断和循环
- PowerShell 条件判断和循环
- python 条件判断和循环
- Windows git和repo下载Android源代码
- Javascript到PHP RSA加密通讯的简单实现
- 【转】常用排序算法总结
- 变态的
- XFire完整入门教程
- 不用循环和条件判断打印1-1000
- 学习嵌入式要人带的原因
- c++版模拟银行窗口排队叫号系统
- linux之 条件测试 和 控制语句概述
- xcode 4 安装com.apple.adc.documentation.AppleiOS4_3.iOSLibrary.docset
- 点子:自动作曲软件
- linux之 线程 网络相关配置简略介绍 和 文件查找
- 确保数据安全是云计算取信于用户的关键
- 贪婪法