TryOmar's blog

By TryOmar, history, 2 years ago, In English

Well, the simple answer is yes. Let me simplify it for you at first.. How can you print from 1 to n without using a loop?

By repeating this line 1000 times, Your program works just as efficiently as if you made an iterative loop from 1 to 1000.

 cout << x++ << endl;
  if(x==n)return 0;

For example, this code can print you numbers from 1 to 10.. You can develop it up to 1000

    int n,x=1;
    cin >> n; n++;
    cout << x++ << endl; // 1
    if(x==n)return 0;
    cout << x++ << endl; // 2
    if(x==n)return 0;
    cout << x++ << endl; // 3
    if(x==n)return 0;
    cout << x++ << endl; // 4
    if(x==n)return 0;
    cout << x++ << endl; // 5
    if(x==n)return 0;
    cout << x++ << endl; // 6
    if(x==n)return 0;
    cout << x++ << endl; // 7
    if(x==n)return 0;
    cout << x++ << endl; // 8
    if(x==n)return 0;
    cout << x++ << endl; // 9
    if(x==n)return 0;
    cout << x++ << endl; // 10
    if(x==n)return 0;

Input : 6 Output : 1 2 3 4 5 6

Well here comes the big question.. How do we print an array in reverse without using a loop? Well simply by reading the variables in ascending order and printing them in descending

Here is a small example on 5 variables:


int n; cin >> n; //Number of Elements (Size of the array) int x1; cin >> x1; if (n == 1) goto line1; int x2; cin >> x2; if (n == 2) goto line2; int x3; cin >> x3; if (n == 3) goto line3; int x4; cin >> x4; if (n == 4) goto line4; int x5; cin >> x5; if (n == 5) goto line5; line5: cout << x5 << ' '; line4: cout << x4 << ' '; line3: cout << x3 << ' '; line2: cout << x2 << ' '; line1: cout << x1 << ' ';

Input: 1 4 6 10 17 Output: 17 10 6 4 1

You can do the same thing up to 1000 elements or more..believe it or not, I solved some problems related to Arrays/Loops in this way.. And It Worked (Accepted✅)

  • Vote: I like it
  • -64
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it +1 Vote: I do not like it

sorry, but actually I think the real expected answer was recursion, you can read the value, call the next recursion first, and then output the variable. Same logic as preorder/inorder/postorder.

»
2 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Why not recursion?

long long recur(long long n){
   cout << n << "\n";
   if(n <= 1) return 0;
   return recur(n-1);
}

int main(){
   long long n; cin >> n;
   recur(n);
   return 0;
}

I don't really know if this is a scam blog or not but I answered it anyway.

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I really don't know why everyone is down-voting me.. I see it as a creative idea.. I really solved a problem with it asking me to reverse and array (and I solved it without an array/loop), and another problem asking me about printing numbers from 1 to n (and I solved it without a loop)

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It is not a creative idea: What if n = 1e18? Would you love to copy-and-paste 1e18 times?

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      lol if n = 1e18 you cannot store them in a array nor print them , it will take 1e9 seconds(~32 years)

  • »
    »
    2 years ago, # ^ |
    Rev. 17   Vote: I like it 0 Vote: I do not like it

    it is not creative and usable , since you can just reverse(array.begin() , array.end()); idk why would someone do that in a contest or a interview

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      At least you can learn something from me is that you can take advantage of programming to write another code with repetitive lines like this

      	for (int i = 0; i < 10; i++)
      	{
      		cout << "cout << x++ << endl;\n";
      		cout << "if (x == n)return 0;\n";
      	}
      
      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        dont get me wrong but these are really common features , everyone uses them , maybe you should check other people's code before writing something like this idk

        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Can you give me an example please?

          • »
            »
            »
            »
            »
            »
            2 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            like for example , we use for loop to do repetative tasks for(int i = 0;i<n;i++)do_something(); you dont need to use goto or other weird and unnecesarily complicated things like that , it will slow you