C: Looping without using looping statements or recursion

前端 未结 16 1835
走了就别回头了
走了就别回头了 2021-02-04 07:56

I want to write a C function that will print 1 to N one per each line on the stdout where N is a int parameter to the function. The function should not use while, for, do-while

16条回答
  •  梦谈多话
    2021-02-04 09:00

    This takes the integer N from the command line and prints out from 1 to N

    #include 
    #include 
    
    int total;
    int N;
    
    int print16(int n)
    {
        printf("%d\n",n+0x01); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x02); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x03); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x04); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x05); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x06); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x07); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x08); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x09); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x0A); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x0B); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x0C); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x0D); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x0E); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x0F); total++; if (total >= N) exit(0);
        printf("%d\n",n+0x10); total++; if (total >= N) exit(0);
    }
    
    int print256(int n)
    {
        print16(n);
        print16(n+0x10);
        print16(n+0x20);
        print16(n+0x30);
        print16(n+0x40);
        print16(n+0x50);
        print16(n+0x60);
        print16(n+0x70);
        print16(n+0x80);
        print16(n+0x90);
        print16(n+0xA0);
        print16(n+0xB0);
        print16(n+0xC0);
        print16(n+0xD0);
        print16(n+0xE0);
        print16(n+0xF0);
    }
    
    int print4096(int n)
    {
        print256(n);
        print256(n+0x100);
        print256(n+0x200);
        print256(n+0x300);
        print256(n+0x400);
        print256(n+0x500);
        print256(n+0x600);
        print256(n+0x700);
        print256(n+0x800);
        print256(n+0x900);
        print256(n+0xA00);
        print256(n+0xB00);
        print256(n+0xC00);
        print256(n+0xD00);
        print256(n+0xE00);
        print256(n+0xF00);
    }
    
    int print65536(int n)
    {
        print4096(n);
        print4096(n+0x1000);
        print4096(n+0x2000);
        print4096(n+0x3000);
        print4096(n+0x4000);
        print4096(n+0x5000);
        print4096(n+0x6000);
        print4096(n+0x7000);
        print4096(n+0x8000);
        print4096(n+0x9000);
        print4096(n+0xA000);
        print4096(n+0xB000);
        print4096(n+0xC000);
        print4096(n+0xD000);
        print4096(n+0xE000);
        print4096(n+0xF000);
    }
    
    int print1048576(int n)
    {
        print65536(n);
        print65536(n+0x10000);
        print65536(n+0x20000);
        print65536(n+0x30000);
        print65536(n+0x40000);
        print65536(n+0x50000);
        print65536(n+0x60000);
        print65536(n+0x70000);
        print65536(n+0x80000);
        print65536(n+0x90000);
        print65536(n+0xA0000);
        print65536(n+0xB0000);
        print65536(n+0xC0000);
        print65536(n+0xD0000);
        print65536(n+0xE0000);
        print65536(n+0xF0000);
    }
    
    int print16777216(int n)
    {
        print1048576(n);
        print1048576(n+0x100000);
        print1048576(n+0x200000);
        print1048576(n+0x300000);
        print1048576(n+0x400000);
        print1048576(n+0x500000);
        print1048576(n+0x600000);
        print1048576(n+0x700000);
        print1048576(n+0x800000);
        print1048576(n+0x900000);
        print1048576(n+0xA00000);
        print1048576(n+0xB00000);
        print1048576(n+0xC00000);
        print1048576(n+0xD00000);
        print1048576(n+0xE00000);
        print1048576(n+0xF00000);
    }
    
    int print268435456(int n)
    {
        print16777216(n);
        print16777216(n+0x1000000);
        print16777216(n+0x2000000);
        print16777216(n+0x3000000);
        print16777216(n+0x4000000);
        print16777216(n+0x5000000);
        print16777216(n+0x6000000);
        print16777216(n+0x7000000);
        print16777216(n+0x8000000);
        print16777216(n+0x9000000);
        print16777216(n+0xA000000);
        print16777216(n+0xB000000);
        print16777216(n+0xC000000);
        print16777216(n+0xD000000);
        print16777216(n+0xE000000);
        print16777216(n+0xF000000);
    }
    
    int print2147483648(int n)
    {
       /*
        * Only goes up to n+0x70000000 since we
        * deal only with postive 32 bit integers
        */
       print268435456(n);
       print268435456(n+0x10000000);
       print268435456(n+0x20000000);
       print268435456(n+0x30000000);
       print268435456(n+0x40000000);
       print268435456(n+0x50000000);
       print268435456(n+0x60000000);
       print268435456(n+0x70000000);
    }
    
    
    int main(int argc, char *argv[])
    {
       int i;
    
       if (argc > 1) {
          N = strtol(argv[1], NULL, 0);
       }
    
       if (N >=1) {
          printf("listing 1 to %d\n",N);
          print2147483648(0);
       }
       else {
          printf("Must enter a postive integer N\n");
       }
    }
    

提交回复
热议问题