Display numbers from 1 to 100 without loops or con

2020-01-24 18:55发布

Is there a way to print numbers from 1 to 100 without using any loops or conditions like "if"? We can easily do using recursion but that again has an if condition. Is there a way to do without using "if" as well? Also no repetitive print statements, or a single print statement containing all the numbers from 1 to 100.

A solution in Java is preferable.

标签: java loops
30条回答
唯我独甜
2楼-- · 2020-01-24 19:17

Abuse an exception to serve as a condition.

public class Main {
    private static int[] stopper = new int[100];

    public static void main(String[] args) {
        try {
            print(1);
        } catch(ArrayIndexOutOfBoundsException e) {
            // abuse of try catch
        }
    }

    private static void print(int i) {
        System.out.println(i);
        stopper[i] = i;
        print(i + 1);
    }
}
查看更多
祖国的老花朵
3楼-- · 2020-01-24 19:18

Pseudo code. Uses an array to force an exception after 100 elements which is caught and does nothing.

function r(array a, int index){
    a[index] = a[index-1]+1
    print a[index]
    r(a, index+1)
}

try{
    array a;
    a.resize(101)
    r(a, 1)
}catch(OutOfBoundsException){
}

EDIT
Java code:

public void printTo100(){
    int[] array = new int[101];
    try{
        printToArrayLimit(array, 1);
    }catch(ArrayIndexOutOfBoundsException e){
    }
}
public void printToArrayLimit(int[] array, int index){
    array[index] = array[index-1]+1;
    System.out.println(array[index]);
    printToArrayLimit(array, index+1);
}
查看更多
Ridiculous、
4楼-- · 2020-01-24 19:19

Here is one using a thread (I inflated the sleep time to account for fluctuations in system speed). I couldn't think of a way to get rid of the try / catch:

public class Counter extends Thread{

    private int cnt;

    public Counter(){
        this.cnt = 0;
    }

    private void increment(){
        System.out.println(cnt++);
        try{
            Thread.sleep(1000);
        }catch(Exception e){}
        increment();
    }

    public void run(){
        increment();
    }

    public static void main(String[] args) throws Exception{
        Counter cntr = new Counter();
        cntr.start();
        cntr.join(100000);
        cntr.interrupt();
        System.exit(0);
    }

}
查看更多
对你真心纯属浪费
5楼-- · 2020-01-24 19:20

DO NOT DO THIS UNDER ANY SANE CIRCUMSTANCES!

public class Fail {

    public void thisFails(int x){
        System.out.println(x);
        Integer[] bigArray = new Integer[9450];
        thisFails(x+1);
    }

    public static void main(String[] args) {
        Fail failure = new Fail();
        failure.thisFails(1);
    }
}

When this is ran using 1m of heap space (java -Xmx1m Fail) it will run out of heap at the 100th recursion.

...

I will now go wash my hands.

查看更多
Lonely孤独者°
6楼-- · 2020-01-24 19:22

In C++:

#include <iostream>

class a {
  static unsigned i;

public:
  a() {
    std::cout << ++i << std::endl;
  }
};

unsigned a::i = 0U;

int main() {
  a array[100];
}

This solution neither uses loops nor recursion for printing numbers from 1 to 100.

查看更多
Juvenile、少年°
7楼-- · 2020-01-24 19:22

Is there a way to print numbers from 1 to 100 without using any loops or conditions like "if"?

Using an optimized version of this:

System.out.println("1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100"); 

Next question?

查看更多
登录 后发表回答