on
21 09 16 과제
21 09 16 과제
1~n 정수 모두 더하기
int SumRecursive(int n) { if (n <= 0) { return n; } return n + SumRecursive(n - 1); }
입력값이 9일때
구구단
void GuGuDan(int gu, int dan) { Console.WriteLine("{0} * {1} = {2} ", gu, dan, gu * dan); if(dan <= 1 ) { return; } GuGuDan(gu, dan - 1); }
팩토리얼
int Factorial(int n) { if(n <= 1 ) { return 1; } return n * Factorial(n - 1); }
입력값이 7일때
마지막 노드 구하기
namespace HelloWorld { class App { Node head; Node temp; //생성자 public App() { head = new Node(3); head.next = new Node(4); head.next.next = new Node(5); temp = head; Console.WriteLine(GetLastNode().data); } Node GetLastNode() { temp = temp.next; if(temp.next == null) { return temp; } return GetLastNode(); } } }
최소값 최대값 구하기
namespace HelloWorld { class App { int[] arr = { 4, 1, 6, 28, 3, 10 }; int max; int min; //생성자 public App() { max = int.MinValue; min = int.MaxValue; Console.WriteLine(GetMax()); Console.WriteLine(GetMin()); } int GetMax(int n = 0) { if (n == arr.Length) { return max; } if (max < arr[n]) { max = arr[n]; } return GetMax(n + 1); } int GetMin(int n = 0) { if(n == arr.Length) { return min; } if(min > arr[n]) { min = arr[n]; } return GetMin(n + 1); } } }
from http://hayedak2.tistory.com/126 by ccl(A) rewrite - 2021-09-17 10:27:14