Untitled

 avatar
unknown
plain_text
5 years ago
10 kB
3
Indexable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApp4
{
    class Program : SystemException
    {
        static bool IsPrime(int num)
        {
            if (num <= 1)
                return false;
            if (num == 2)
                return true;
            if (num % 2 == 0)
                return false;
            for (int i = 3; i < num / 2; i += 2)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

        static int CountPrimeSetBits(int l, int r)
        {
            int count = 0;
            int answer = 0;
            if (l > r)
                throw new SystemException("Invalid range ! Watch out !!");
            if (l == r)
            {
                while (l != 0)
                {
                    if (l % 2 == 1)
                    {
                        ++count;
                        l /= 2;
                    }
                    else
                    {
                        l /= 2;
                    }
                }
                if (IsPrime(count))
                {
                    ++answer;
                }
                return answer;
            }
            int l1 = 0;
            while (l <= r)
            {
                l1 = l;
                while (l1 != 0)
                {
                    if (l1 % 2 == 1)
                    {
                        ++count;
                        l1 /= 2;
                        Console.Write(count.ToString());
                    }
                    else
                    {
                        l1 /= 2;
                        Console.Write(count.ToString());
                    }
                }
                Console.WriteLine();
                if (IsPrime(count))
                {
                    Console.WriteLine("count is prime: " + count.ToString());
                    ++answer;
                }
                ++l;
                count = 0;
            }
            return answer;
        }

        static bool JudgeSquareSum(int c)
        {
            if (c < 0)
                return false;
            if (c == 0)
                return true;
            for (int i = 1; i < c / 2; ++i)
            {
                if (i * i == c)
                    return true;
            }
            double x = (double)c;
            for (int i = 1; i < c / 2; ++i)
            {
                double s = (double)i;
                if (Math.Sqrt(x - s * s) - (int)(Math.Sqrt(x - s * s)) == 0)
                {
                    return true;
                }
            }
            return false;
        }

        static bool IsAnagram(string x, string y)
        {
            if (x.Length != y.Length)
                return false;
            char[] characters = x.ToCharArray();
            Array.Sort(characters);
            string x1 = new string(characters);
            char[] characters1 = y.ToCharArray();
            Array.Sort(characters1);
            string y1 = new string(characters1);
            return x1 == y1;
        }

        static int AdjacentElementProduct(int[] num)
        {
            int max = num[0] * num[1];
            for (int i = 1; i < num.Length - 1; ++i)
            {
                if (max < num[i] * num[i + 1])
                {
                    max = num[i] * num[i + 1];
                }
            }
            return max;
        }

        static int ShapeArea(int n)
        {
            if (n == 1)
                return 1;
            return 4 * (n - 1) + ShapeArea(n - 1);
        }

        static bool AlmostIncreasingSequence(int[] nums)
        {
            int l = nums.Length;
            int p = -1;
            int c = 0;
            for (int i = 1; i < l; ++i)
            {
                if (nums[i - 1] >= nums[i])
                {
                    p = i;
                    ++c; // esi jokel em
                }
            }
            if (c > 1)
                return false;
            if (c == 0)
                return true;
            if (p == (l - 1) || p == 1)
                return true;
            if (nums[p - 1] < nums[p + 1])
                return true;
            if (nums[p - 2] < nums[p])
                return true;
            return false;
        }

        static int FindNthDigit(int n)
        {
            int[] desc = { 0 };
            if (n <= 9)
                return n;
            int b = 9, digits = 1;
            while (n - b * digits > 0)
            {
                n -= b * digits;
                b *= 10;
                digits++;
            }
            int index = n % digits;
            if (index == 0)
                index = digits;
            int num = 1;
            for (int i = 1; i < digits; i++)
                num *= 10;
            num += (index == digits) ? n / digits - 1 : n / digits; ;

            // step 3. find out which digit in the number is we wanted.
            for (int i = index; i < digits; i++)
                num /= 10;
            return num % 10;
        }

        static string reverseParentheses(string s)
        {
            int begin = 0;
            int end = s.Length - 1;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '(')
                    begin = i;
                if (s[i] == ')')
                {
                    end = i;
                    string temp = s.Substring(begin + 1, end - begin - 1);
                    char[] c = temp.ToCharArray();
                    Array.Reverse(c);
                    c.ToString();
                    string temp1 = new string(c);
                    //return reverseParentheses(s.Substring(0, begin) + String.Join("", temp.Reverse()) + s.Remove(0, end + 1));
                    return reverseParentheses(s.Substring(0, begin) + temp1 + s.Substring(end + 1));
                }

            }
            return s;
        }

        static int[] polynomialMultiplication(int[] polynom1, int[] polynom2)
        {
            int psize = polynom1.Count() + polynom2.Count() - 1;
            int[] prod = new int[psize];
            for (int i = 0; i < polynom1.Count(); i++)
            {
                for (int j = 0; j < polynom2.Count(); j++)
                {
                    prod[i + j] += polynom1[i] * polynom2[j];
                }
            }
            return prod;
        }

        static string[] addBorder(string[] picture)
        {
            int size = picture[0].Length + 2;
            string first = "";
            for (int i = 0; i < size; ++i)
                first += "*";
            string[] ans = new string[picture.Length + 2];
            ans[0] = first;
            ans[picture.Length + 1] = first;
            for(int i = 1; i < picture.Length; ++i)
            {
                ans[i] = string.Concat('*', picture[i - 1],'*');
            }
            return ans;
        }

        static int MaximumElementsProduct(int[] num)
        {
            int max = num[0] * num[1];
            for (int i = 0; i < num.Length; ++i)
            {
                for(int j = 0; j < num.Length && i != j; ++j)
                {
                    if (max < num[i] * num[j])
                    {
                        max = num[i] * num[j];
                    }
                }
            }
            return max;
        }

        static bool palindromeRearranging(string inputString)
        {
            char[] str = inputString.ToCharArray();
            Array.Sort(str);
            int i = 0, j = 0;
            int p = 0;
            int i1 = str.Length + 1;
            if (str.Length % 2 == 0)
            {
                while(i < str.Length)
                {
                    if(str[i] == str[i+1])
                    {
                        i += 2;
                    }
                    else
                    {
                        j = -1;
                    }
                if (j == -1)
                {
                    return false;
                }
                }
            }
            else
            {
                while (i + 1 < str.Length)
                {
                    if (str[i] == str[i + 1])
                    {
                        i += 2;
                    }
                    else
                    {
                        i1 = i + 1; 
                        ++p;
                        break;
                    }
                }
                while(i1 < str.Length - 1)
                {
                    if (str[i1] == str[i1 + 1])
                    {
                        i1 += 2;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        static bool isIPv4Address(string inputString)//failed
        {
            string con = inputString + '.';
            string[] array = con.Split('.');
            if (array.Length != 4)
            {
                return false;
            }
            int[] numbers = { -1 };
            for (int i = 0; i < array.Length - 1; ++i)
            {
                numbers[i] = Int32.Parse(array[i]);
            }
            int count = 0;
            for (int i = 0; i < array.Length - 1; ++i)
            {
                if (numbers[i] >= 0 && numbers[i] <= 255)
                    ++count;
            }
            return (count == 4);

        }



        static void Main(string[] args)
        {
            string inputString = "172.16.254.1";
            string inputstrinh = "172.196.254.1";
            string con = inputString + '.';
            string[] array = con.Split('.');
            /*for(int i = 0; i < array.Length - 1; ++i)
            {
                Console.WriteLine(array[i]);
            }*/
            Console.WriteLine(array[4]);
            return;
        }
    }
}

Editor is loading...