Advertisement
AvengersAssemble

Untitled

Nov 19th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace RecursionExercise
  7. {
  8.     class Program
  9.     {
  10.         //Exc1
  11.         //טענת כניסה: הפעולה מקבלת תו ומספר שלם אי-שליל
  12.         //טענת יציאה: הפעולה מחזירה את התו רשום בשורה מספר פעמים כגודל המספר השלם
  13.         public static void Mystery1(char ch, int n)
  14.         {
  15.             if (n > 0)
  16.             {
  17.                 Console.Write(ch);
  18.                 Mystery1(ch, n - 1);
  19.             }
  20.         }
  21.  
  22.         //Exc3
  23.         //טענת כניסה: הפעולה מקבל מספר שלם חיובי
  24.         //טענת יציאה: הפעולה מחזירה את הספרה הגדולה ביותר במספר
  25.         public static int Mystery2(int n)
  26.         {
  27.             if (n < 10)
  28.                 return n;
  29.             else
  30.             {
  31.                 int x = n % 10;
  32.                 int y = Mystery2(n / 10);
  33.                 if (x > y)
  34.                     return x;
  35.                 else
  36.                     return y;
  37.             }
  38.         }
  39.  
  40.         //Exc5
  41.         //טענת כניסה: הפעולה מקבלת מערך של מספרים שלמים שאינו ריק, ומספר שלם נוסף וחיובי הקטן או שווה לגודל המערך
  42.         //הפעולה מחזירה את ממוצע האיברים במערך עד לאיבר הנמצא במקום של המספר
  43.         public static float Mystery3(int[] a, int k)
  44.         {
  45.             float x;
  46.             if (k == 1)
  47.                 return a[0];
  48.             x = Mystery3(a, k - 1) * (k - 1);
  49.             return ((a[k - 1] + x) / k);
  50.         }
  51.         //Exc7
  52.         //טענת כניסה: הפעולה מקבלת שניע מספרים שלמים, המספר השני הוא גם אי-שלילי
  53.         //טענת יציאה: הפעולה מחזירה את מכפלת שני המספרים
  54.         public static int Mystery4(int a, int b)
  55.         {
  56.             if (b == 0)
  57.                 return 0;
  58.             if (b % 2 == 0)
  59.                 return Mystery4(a + a, b / 2);
  60.             return Mystery4(a + a, b / 2) + a;
  61.         }
  62.  
  63.         //Exc9
  64.         public static int NumDigits(int num)
  65.         {
  66.             if (num < 10)
  67.                 return 1;
  68.             return 1 + NumDigits(num / 10);
  69.         }
  70.  
  71.         //Exc11
  72.         public static bool IsDigitExist(int num, int digit)
  73.         {
  74.             bool ans = num % 10 == digit;
  75.             if(num < 10)
  76.                 return ans;
  77.             return false || IsDigitExist(num / 10, digit);
  78.         }
  79.  
  80.         //Exc13
  81.         //ג
  82.         public static int Mod(int n, int m)
  83.         {
  84.             if (n < m)
  85.                 return n;
  86.             else
  87.                 return Mod(n - m, m);
  88.         }
  89.         //Exc17
  90.         public static int CountDigs(int num)
  91.         {
  92.             if (num < 10)
  93.                 return 1;
  94.             return 1 + CountDigs(num / 10);
  95.         }
  96.  
  97.         //Exc19
  98.         public static int Sum(int num)
  99.         {
  100.             if (num < 10)
  101.                 return num;
  102.             return num % 10 + Sum(num / 10);
  103.         }
  104.         public static double Avg(int num)
  105.         {
  106.             return (double)Sum(num) / CountDigs(num);
  107.         }
  108.  
  109.         //Exc21
  110.         public static void NumPrefix(int num)
  111.         {
  112.             if (num > 10)
  113.             {
  114.                 Console.WriteLine(num / 10);
  115.                 NumPrefix(num / 10);
  116.             }
  117.         }
  118.  
  119.         //Exc23
  120.         public static int i = 0;
  121.         public static int SumArr(int[] arr)
  122.         {
  123.             i++;
  124.             if (i == arr.Length - 1)
  125.             {
  126.                 i = 0;
  127.                 return arr[i];
  128.             }
  129.             return arr[i] + SumArr(arr);
  130.         }
  131.  
  132.         //Exc25
  133.         public static bool CheckArr(int[] arr)
  134.         {
  135.             i++;
  136.             if (i == arr.Length - 2)
  137.             {
  138.                 if (arr[i + 1] - arr[i] == arr[i] - arr[i - 1])
  139.                     return true;
  140.             }
  141.             if (arr[i + 1] - arr[i] == arr[i] - arr[i - 1])
  142.                 return true && CheckArr(arr);
  143.             return false;
  144.         }
  145.  
  146.         //Exc27
  147.         public static void PrintTriangle(int n)
  148.         {
  149.             if (n == 1)
  150.                 Console.WriteLine("*");
  151.             else
  152.             {
  153.                 PrintTriangle(n - 1);
  154.                 for (int i = 0; i < n; i++)
  155.                     Console.Write("*");
  156.                 Console.WriteLine();
  157.             }
  158.         }
  159.  
  160.         //Exc29
  161.         public static void Permutation(int[] arr)
  162.         {
  163.  
  164.         }
  165.  
  166.         static void Main(string[] args)
  167.         {
  168.             int[] arr = new int[4] { 1, 3, 8, 10 };
  169.             Console.WriteLine(Mystery4(2, 3));
  170.             //Console.WriteLine(CountDigs(42432));
  171.             //Console.WriteLine(Avg(4324));
  172.             //NumPrefix(29807);
  173.             //Console.WriteLine(SumArr(arr));
  174.             //Console.WriteLine(CheckArr(arr));
  175.             //PrintTriangle(4);
  176.  
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement