首页 > 其他分享 >不用循环和条件判断打印1-1000

不用循环和条件判断打印1-1000

时间:2023-06-09 16:38:11浏览次数:27  
标签:include int void 打印 循环 exit main 1000

//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

标签:include,int,void,打印,循环,exit,main,1000
From: https://blog.51cto.com/u_16156420/6449052

相关文章

  • java不打印异常堆栈
    背景:生产环境抛异常,但却没有将堆栈信息输出到日志,只有简单的java.lang.NullPointerException错误信息。原因分析JVM在默认启动的时候会加上OmitStackTraceInFastThrow参数,含义是当大量抛出同样的异常的后,后面的异常输出将不打印堆栈。原因是打印堆栈的时候底层会调用到Throw......
  • #Vue-cli 5 怎么打印代理地址 #Vue-cli 5 怎么查看真是请求地址 ?
    proxy:{'/api':{target:'http://192.168.3.47:1228',changeOrigin:true,onProxyReq:function(proxyReq,req,res){console.log('[proxy]:'+proxyReq.getHeader('origin')+proxyReq.p......
  • Linux内核之 printk 打印
    前言printk在内核源码中用来记录日志信息的函数,只能在内核源码范围内使用,用法类似于printf函数。printk函数主要做两件事情:1.将信息记录到log中;2.调用控制台驱动来将信息输出。一、printk介绍printk将内核信息输出到内核信息缓冲区中,内核缓冲区在kernel/printk/p......
  • Jmeter 循环控制器与计数器
    图1 图2  图3 图4: 图5:  图6:   ......
  • 【Java查漏补缺(一)】数组与循环
    除了数组与循环,还有方法,讲究看吧!后续练习内容都是连贯的!BasicalJava看下Java中的变量类型吧!数据类型关键字内存占用二进制位数字节型byte1个字节4位短整型short2个字节8位整型int(常用)4个字节32位长整型long8个字节64位单精度浮点数float......
  • QQ登录报错:redirect uri is illegal(100010)解决方案
    查看真实回调地址:通过腾讯开放平台:腾讯开放平台官网进入后,点击RUL编解码工具:在输入源串的表格里填入你登录QQ报错的网址(网址全部复制进去),点击URL解码:解码后,能查看到实际回调地址:实际回调地址就是redirect_uri=之后到第一个&符号或者问号截止的部分(也就是用矩形圈起来的地方),把这......
  • 打印凌形
    intmain(){ intline=0; scanf("%d",&line);//输入上半部分的行数 inti=0; //先打印上半部分 for(i=0;i<line;i++) { intj=line-1-i;//打印空格的数量 while(j) { printf(""); j--; } intk=2*i+1;//打印*的数量 while(k) { printf("*......
  • P2714 循环不变式的基础应用
    你听说过循环不变式吗?不妨来品鉴一下吧:WeLikeStudying大佬的博客:循环不变式而这篇文章,只是对大佬博客的小小注解,外加一点实际应用。我们可以把循环不变式理解成一组条件。在每次循环中,我们保证这组条件均为真。而最后循环就可以得出我们想要的结果。如果思考本质,这实际上......
  • JS引擎中的线程,事件循环,上下文
     线程浏览器中有哪些进程呢?1.浏览器进程:浏览器的主进程,负责浏览器的界面界面显示,与用户交互,网址栏输入、前进、后退,以及页面的创建和销毁。2.渲染进程(浏览器内核):默认一个tab页面一个渲染进程,主要的作用为页面渲染,脚本执行,事件处理等。3.GPU进程:用于3D绘制等,将开启了3D绘制......
  • 【python基础】循环语句-break关键字
    1.break关键字break关键字,其作用是在循环中的代码块遇到此关键字,立刻跳出整个循环,执行循环外的下一条语句。其在while和for循环中的作用示意图如下:1.1break在while循环中的使用1.1.1不加else语句比如我们通过键盘输入单词,输出刚才的单词,编写程序如下所示:我们发现当我们输......