Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace RecursionExercise
- {
- class Program
- {
- //Exc1
- //טענת כניסה: הפעולה מקבלת תו ומספר שלם אי-שליל
- //טענת יציאה: הפעולה מחזירה את התו רשום בשורה מספר פעמים כגודל המספר השלם
- public static void Mystery1(char ch, int n)
- {
- if (n > 0)
- {
- Console.Write(ch);
- Mystery1(ch, n - 1);
- }
- }
- //Exc3
- //טענת כניסה: הפעולה מקבל מספר שלם חיובי
- //טענת יציאה: הפעולה מחזירה את הספרה הגדולה ביותר במספר
- public static int Mystery2(int n)
- {
- if (n < 10)
- return n;
- else
- {
- int x = n % 10;
- int y = Mystery2(n / 10);
- if (x > y)
- return x;
- else
- return y;
- }
- }
- //Exc5
- //טענת כניסה: הפעולה מקבלת מערך של מספרים שלמים שאינו ריק, ומספר שלם נוסף וחיובי הקטן או שווה לגודל המערך
- //הפעולה מחזירה את ממוצע האיברים במערך עד לאיבר הנמצא במקום של המספר
- public static float Mystery3(int[] a, int k)
- {
- float x;
- if (k == 1)
- return a[0];
- x = Mystery3(a, k - 1) * (k - 1);
- return ((a[k - 1] + x) / k);
- }
- //Exc7
- //טענת כניסה: הפעולה מקבלת שניע מספרים שלמים, המספר השני הוא גם אי-שלילי
- //טענת יציאה: הפעולה מחזירה את מכפלת שני המספרים
- public static int Mystery4(int a, int b)
- {
- if (b == 0)
- return 0;
- if (b % 2 == 0)
- return Mystery4(a + a, b / 2);
- return Mystery4(a + a, b / 2) + a;
- }
- //Exc9
- public static int NumDigits(int num)
- {
- if (num < 10)
- return 1;
- return 1 + NumDigits(num / 10);
- }
- //Exc11
- public static bool IsDigitExist(int num, int digit)
- {
- bool ans = num % 10 == digit;
- if(num < 10)
- return ans;
- return false || IsDigitExist(num / 10, digit);
- }
- //Exc13
- //ג
- public static int Mod(int n, int m)
- {
- if (n < m)
- return n;
- else
- return Mod(n - m, m);
- }
- //Exc17
- public static int CountDigs(int num)
- {
- if (num < 10)
- return 1;
- return 1 + CountDigs(num / 10);
- }
- //Exc19
- public static int Sum(int num)
- {
- if (num < 10)
- return num;
- return num % 10 + Sum(num / 10);
- }
- public static double Avg(int num)
- {
- return (double)Sum(num) / CountDigs(num);
- }
- //Exc21
- public static void NumPrefix(int num)
- {
- if (num > 10)
- {
- Console.WriteLine(num / 10);
- NumPrefix(num / 10);
- }
- }
- //Exc23
- public static int i = 0;
- public static int SumArr(int[] arr)
- {
- i++;
- if (i == arr.Length - 1)
- {
- i = 0;
- return arr[i];
- }
- return arr[i] + SumArr(arr);
- }
- //Exc25
- public static bool CheckArr(int[] arr)
- {
- i++;
- if (i == arr.Length - 2)
- {
- if (arr[i + 1] - arr[i] == arr[i] - arr[i - 1])
- return true;
- }
- if (arr[i + 1] - arr[i] == arr[i] - arr[i - 1])
- return true && CheckArr(arr);
- return false;
- }
- //Exc27
- public static void PrintTriangle(int n)
- {
- if (n == 1)
- Console.WriteLine("*");
- else
- {
- PrintTriangle(n - 1);
- for (int i = 0; i < n; i++)
- Console.Write("*");
- Console.WriteLine();
- }
- }
- //Exc29
- public static void Permutation(int[] arr)
- {
- }
- static void Main(string[] args)
- {
- int[] arr = new int[4] { 1, 3, 8, 10 };
- Console.WriteLine(Mystery4(2, 3));
- //Console.WriteLine(CountDigs(42432));
- //Console.WriteLine(Avg(4324));
- //NumPrefix(29807);
- //Console.WriteLine(SumArr(arr));
- //Console.WriteLine(CheckArr(arr));
- //PrintTriangle(4);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement