I was trying to solve a regression problem from baekjoon algorithm.,
I was a little lazy studying these days, not because I am already good at it, but I met many people.
I met two geniuses who are naturally smart enough to get the first status in their career. They were both younger than me but achieved so many competitive skills and philosophical thoughts. Even though they were younger, I called them teachers and ask them to teach me how I should manage my time, etc.
They told me Time is infinite and not scarce if we never give up and constantly learn. I was shocked by their sayings. I was always followed by this time scarcity but I don't think I achieved many. They had no fear, they simply did what they had to and enjoyed their free time also without obsessive goals.
I, now, try to change my attitude of studying all day to focus effectively and enjoy my life with managing my body and meeting new people, and having new experiences too.
and This is the first question I am trying to solve after those lessons. I am not going to be too harsh on myself when learning a new thing but, only try to focus and understand one by one without much stress.
Now after reading factorial problem I couldn't understand what factorial is but could guess what it means. I thought I could use for statement for this, as N! means an integer of 1~N is timed altogether.
So I made this code below and failed.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int fac = 0;
for (int i=1;i<=A;i++) {
int b = i*(i+1);
fac =+ b;
}
System.out.println(fac);
}
}
Recursive HelloWorld method
package recursive;
public class PlusFuction {
public static void main(String[] args) {
HelloWorld(5);
}
public static void HelloWorld(int n) {
if(n==0)
return;
System.out.println("HelloWorld");
HelloWorld(n-1);
}
}
explanation :
when n == 0, the method ends.
every time method is called, n - 1 is operated.
Print the total sum of 1 to N
package recursive;
import java.util.Scanner;
public class PlusFunction2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
// Calling PlusPlus method
System.out.println(PlusPlus(N));
}
// Declaration of PlusPlus method
public static int PlusPlus(int n) {
// when n == 0 return
if(n==0)
return 0;
return n += PlusPlus(n-1); // start recursion
}
}
'Programming > Java' 카테고리의 다른 글
3월 12일 배운 것 (0) | 2022.03.14 |
---|---|
Java Strings Tutorial (0) | 2022.02.21 |
Linked List (0) | 2022.02.10 |
퀵 정렬 자바 (0) | 2022.02.10 |
HashTable in java (0) | 2022.02.10 |