当前位置:网站首页>C method question 2

C method question 2

2022-07-07 23:34:00 Bobo in summer

13 Write a method , Fractional sequence :2/1,1/3,3/4,4/7,7/11,11/18… Before 10 Sum of items , And back to . Require application writing , To test the correctness of the method .

 public static double summ()
        {
    
            double sum = 0;
            double a = 2;
            double b = 1;
            double temp;
            for (int i = 0;i<10;i++)
            {
    
                sum += a / b;
                temp = b; 
                b = a + b;
                a = temp;
            }
            return sum;
        }
 Console.WriteLine(summ());

14. Write a method , seek 1+1/2!+1/3!+…+1/n! And , And return the sum as the return value , Require application writing , To test the correctness of the method .

  public static double summ(double a)
        {
    
             double sum = 0;
             double b = 1;
            double temp = 0;
             for(int i = 1;i<=a;i++)
            {
    
                b = b * i;
                temp = 1 / b;
                sum += temp;
            }
             
            return sum;

        }
  double aaa =double.Parse( Console.ReadLine());
            Console.WriteLine(summ(aaa));

15. Write a method , Yes 4 Bit integer encryption , The encryption rules are as follows : Add... To every number 7, Then divide the sum by 10 The remainder of replace the number , Then exchange the first and second positions , The fourth and third bits are exchanged , This method returns the encrypted number . Require application writing , To test the correctness of the method .

 public static int JiaMi(int n)
        {
    
            int mima = 0;
            int gw = (n % 10 + 7) % 10;
            int sw = (n / 10 % 10 + 7) % 10;
            int bw = (n / 100 % 10 + 7) % 10;
            int qw = (n / 1000 + 7) % 10;
            mima = bw * 1000 + qw * 100 + gw * 10 + sw;
            return mima;
        }
  public static void Main(string[] args)
        {
    
            Console.WriteLine(" Please enter a four digit password :");
            int nnn = int.Parse(Console.ReadLine());
            Console.WriteLine(" The result after encryption is :");
            Console.WriteLine(JiaMi(nnn));
        }

17. Write a method , Find the longest word in a string , Words are separated by spaces , And return the longest word as the method return value . Require application writing , To test the correctness of the method .

  public static string MaxWord(string ss)
        {
    
            string s = string.Empty;
            string[] words = new string[100];
            words = ss.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
    
                if (s.Length < words[i].Length)
                {
    
                    s = words[i];
                }
            }
            return s;
        }
 public static void Main(string[] args)
        {
    
            Console.WriteLine(" Please enter a string :");
            string st = Console.ReadLine();
            Console.WriteLine(" The longest word is :");
            Console.WriteLine(MaxWord(st));
            Console.ReadLine();
        }

18. Write a method , For a given date , Return the day of the week . for example 2002-3-28 Back to Thursday . Require application writing , To test the correctness of the method .

 public static void Rdate(string date)
        {
    
            string weekst = DateTime.Parse(date).DayOfWeek.ToString();
            switch (weekst)
            {
    
                case "Monday": weekst = " Monday "; break;
                case "Tuesday": weekst = " Tuesday "; break;
                case "Wednesday": weekst = " Wednesday "; break;
                case "Thursday": weekst = " Thursday "; break;
                case "Friday": weekst = " Friday "; break;
                case "Saturday": weekst = " Saturday "; break;
                case "Sunday": weekst = " Sunday "; break;
                default: Console.WriteLine(" The input data is illegal "); break;
            }
            Console.WriteLine(weekst);
        }
            string datetimes = Console.ReadLine();
            Rdate(datetimes);     

19. Write a method , Randomly generated 10 individual [20,50] The positive integer of is stored in the array , And output the maximum value of all elements in the array 、 minimum value 、 Average value and sum of all elements . Require application writing , To test the correctness of the method .

  public static void MaxMinAvg(out int max, out int min, out double avg, out int sum)
        {
    
            Random ran = new Random();
            int[] arr = new int[10];
            max = 0;
            min = 50;
            sum = 0;
            Console.WriteLine(" produce 10 individual [20,50] The random number sequence between is :");
            for (int i = 0; i < arr.Length; i++)
            {
    
                arr[i] = ran.Next(20, 51);
                sum += arr[i];
                if (arr[i] < min)
                {
    
                    min = arr[i];
                }
                if (arr[i] > max)
                {
    
                    max = arr[i];
                }
                Console.Write("{0} ", arr[i]);
            }
            avg = sum / 10;
        }
  public static void Main(string[] args)
        {
    
            int max;
            int min;
            double avg;
            int sum;
            MaxMinAvg(out max, out min, out avg, out sum);
            Console.WriteLine(" Maximum :{0}, minimum value :{1}, Average :{2}, and :{3}", max, min, avg, sum);
            Console.ReadLine();
        }

20. It is known that the first two terms of a sequence are 1,2, The following items are the sum of the first two adjacent items , Write a method , Calculate and return to the front of the sequence n Sum of square roots of terms sum. Require application writing , To test the correctness of the method .

public static double nSum(int n)
        {
    
            int[] arr = new int[n];
            arr[0] = 1;
            arr[1] = 2;
            double sum = 0;
            for(int i = 2; i <arr.Length; i++)
            {
    
                arr[i] = arr[i - 1] + arr[i - 2];      
            }
            for(int i = 0; i < arr.Length; i++)
            {
    
                sum += Math.Sqrt(arr[i]);
            }
            return sum;
        }
  Console.WriteLine(" Please input before n term n Value :");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine( nSum(n));  
            Console.ReadLine();

21. Write a method , Judge whether a number can be 3 Divide but not be 7 to be divisible by , Write applications , Output 1-100 All within can be 3 Divide but not be 7 Divisible number . Require application writing , To test the correctness of the method .

  public static void Jisuan(int a)
        {
    
            if (a % 3 == 0 && (a % 7) != 0)
            {
    
                Console.WriteLine(" Sure ");
            }
            else
            {
    
                Console.WriteLine(" no way ");
            }
            Console.WriteLine("1-100 There are these :");
            for (int i = 1;i<=100;i++)
            {
    
               
                if (i % 3 == 0 && (i % 7)!=0)
                {
    
                    Console.WriteLine(i);
                }

            }
          
        }
  Console.WriteLine(" Please enter :");
            int aaa = int.Parse(Console.ReadLine());
            Jisuan(aaa);

22. Write a method , Calculation 1 To n Sum the squares of all numbers between , Require application writing , To test the correctness of the method .

  public static double summ(int a)
        {
    
            double b = 0;
            double c = 0;
            for (int i = 1;i<=a;i++)
            {
    
                c = i * i;
                b += c;
            }
            return b;
        }
 int aaa = int.Parse(Console.ReadLine());
            Console.WriteLine(summ(aaa));

23. Write a method , Judge whether a three digit number is equal to the cubic sum of each digit , for example 153=13+53+33, Require application writing , To test the correctness of the method . Require application writing , To test the correctness of the method .

 public static void lifanghe(int a)
        {
    

            if (((a / 100)* (a / 100)* (a / 100) + (a % 100 / 10)* (a % 100 / 10)* (a % 100 / 10) + (a % 10)* (a % 10)* (a % 10)) == a)
            {
    
                Console.WriteLine(" accord with ");
            }
            else
            {
    
                Console.WriteLine(" Do not conform to the ");
            }
          
        }
  int aaa =int.Parse( Console.ReadLine());
            lifanghe(aaa);   
        }
原网站

版权声明
本文为[Bobo in summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130558211993.html