Recursion using main() function

Правка en1, от TryOmar, 2022-06-07 12:07:50

Hello guys.. I was solving a simple problem that asked me to find the sum of n numbers So I thought about solving this problem with recursion And since I don't like making a lot of functions, instead I decided to use the main function. And yeah that program worked in an efficient way ✅ If you want to try it here it is (just don't use freopen)

#include <bits/stdc++.h>

using namespace std;
int n = 0, x;
int main() {
	//Made By Benjamin007
	static long long sum = 0;
	static int i = 0;
	if (n == 0)
		cin >> n;
	i++;
	if (i <= n) {
		cin >> x;
		sum += x;
		if (i == n)cout << sum;
		return main();
	}
	return 0;
}

Теги recursion, easy, new

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский TryOmar 2022-06-07 12:07:50 692 Initial revision (published)