C: Looping without using looping statements or rec

2020-05-23 06:35发布

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 loops, goto statement, recursion, and switch statement. Is it possible?

标签: c loops
16条回答
欢心
2楼-- · 2020-05-23 07:10

write all possible output to a string first, and null terminate it where the output should stop.
this is a rather dirty solution, but given the limitations, all I can think of,
except for using assembler, off course.

char a[]="1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n"/*...*/;
main(n,v)char**v;{n=atoi(v[1]);
#define c(x)(n>x?n-x:0)
a[n+c(1)+c(9)+c(99)+c(999)+c(9999)+c(99999)+c(999999)+c(9999999)/*+...*/]=0;
puts(a);}

Given that MAX_INT==2147483647 on popular architectures, we only need to go up to +c(999999999). Typing out that initial string might take a while, though...

查看更多
混吃等死
3楼-- · 2020-05-23 07:10

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

#include <stdio.h>
#include <stdlib.h>

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");
   }
}
查看更多
疯言疯语
4楼-- · 2020-05-23 07:16

N is not fixed, so you can't unrole the loop. And C has no iterators as far as I know.

You should find something that mimics the loop.

Or thinking outside the box:

(for example N is limited to 1000, but it is easy to adapt)

int f(int N) {
    if (N >= 900) f100(100);
    if (N >= 800) f100(100);
    if (N >= 700) f100(100);
    ...

    f100(n % 100);
}

int f100(int N) {
    if (N >= 90) f10(10);
    if (N >= 80) f10(10);
    if (N >= 70) f10(10);
    ...

    f(n % 10);
}

int f10(int N) {
    if (N >= 9) func();
    if (N >= 8) func();
    if (N >= 7) func();
    ...
}
查看更多
三岁会撩人
5楼-- · 2020-05-23 07:18

I'm very disappointed that this doesn't work. To me, the phrase "a function is called after any previously registered functions that had already been called at the time it was registered" suggests that it is possible to register atexit handlers after they have started to be called. That is, a handler can register another handler. Otherwise, how is it even possible for there to exist a function which has been called at the time another function is registered? But for me the call to atexit is returning 0 success, but not actually resulting in another call. Anyone know why, have I made some silly error?

#include "stdio.h"
#include "stdlib.h"

int count = 0;
int limit = 10;

void handler() {
    printf("%d of %d\n", ++count, limit);
    if (count < limit) atexit(handler);
}

int main(int argc, char **argv) {
    if (argc > 1) limit = atoi(argv[1]);
    atexit(handler);
}

By the way, not recursion because atexit doesn't call its parameter, it queues it to be called later. Obviously the C runtime contains a loop to call atexit handlers, but that loop exists whether you actually register any atexit handlers or not. So if this program contains a loop, so does every C program ;-)

查看更多
▲ chillily
6楼-- · 2020-05-23 07:19

If you know the upper limit of N you can try something like this ;)

void func(int N)
{
    char *data = " 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n";
    if (N > 0 && N < 12)
        printf("%.*s", N*3, data);
    else
        printf("Not enough data. Need to reticulate some more splines\n");
}

Joke aside, I don't really see how you can do it without recursion or all the instructions you mentioned there. Which makes me more curious about the solution.

Edit: Just noticed I proposed the same solution as grombeestje :)

查看更多
再贱就再见
7楼-- · 2020-05-23 07:19
int x=1;

void PRINT_2(int);

void PRINT_1(int n)
{ if(x>n)
    return;
  printf("%d\n",x++);
  PRINT_2(n);  
}

void PRINT_2(int n)
{ if(x>n)
    return;
  printf("%d\n",x++);
  PRINT_1(n);  
}

int main() 
{   int n;
    scanf("%d",&n);
    if(n>0)
      PRINT_1(n);
    system("pause");
}
查看更多
登录 后发表回答