当前位置:网站首页>C simple question one

C simple question one

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

1. Output in reverse order

            string aa = Console.ReadLine();
            string bb="";
            for (int i = aa.Length-1; i >= 0; i--)
            {
    
                bb += aa[i];
            }
            Console.WriteLine(bb);

 Insert picture description here
2. The length of three sides of a triangle is known a,b,c, The three sides are input by the user , Programming judgment a、b、c Whether the value of constitutes a triangle , If it forms a triangle , Then calculate and output the area of the triangle , Otherwise output “ Can't make a triangle ” Calculate the area of the triangle

           double a =double.Parse(Console.ReadLine());
            double b = double.Parse(Console.ReadLine());
            double c = double.Parse(Console.ReadLine());
            double s = 0;
            double area = 0;
            if ((a + b > c) && (a + c > b) && (b + c > a))
            {
    
                Console.WriteLine(" Sure ");
                s = 0.5 * (a + b + c);
                area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
            }
            else
            {
    
                Console.WriteLine(" Can't form a triangle ");
            }

 Insert picture description here
3. Enter a string str1, Delete str1 All of them 0-9 The number character of , Output the processed string

            string aa = Console.ReadLine();
            string bb = "";
            for (int i = 0; i < aa.Length; i++)
            {
    
                if (!char.IsDigit(aa[i]))
                {
    
                    bb += aa[i];
                }
            }
            Console.WriteLine(bb);

 Insert picture description here
4. Input 10 Number , Calculate average , Count the number of data below the average and output the data below the average

    Console.Write(" Please enter ten integers : Press enter Continue to enter ");
            int[] a = new int[10];
            for (int i = 0; i < 10; i++)
            {
    
                a[i] = int.Parse(Console.ReadLine());
            }
            int b = 0;
            for (int j = 0; j < 10; j++)
            {
    
                b += a[j];
            }
            float c = b / 10;
            int count = 0;
            for (int k = 0; k < 10; k++)
            {
    
                if (a[k] < c)
                {
    
                    Console.Write("{0},", a[k]);
                    count++;
                }
            }
            Console.WriteLine(" share {0} A number less than the average ", count);

 Insert picture description here
5. Input 10 Number , Calculate average , Count the number of data higher than the average value and output the data higher than the average value

int[] srr = new int[10] {
     1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            double sum = 0;
            double ave = 0;
            int count = 1;
            for (int i = 0; i < 10; i++)
            {
    
                sum += srr[i];
                count++;
            }
            ave = sum / count;
            string aa = "";
            int count1 = 1;
            for (int j = 0; j < 10; j++)
            {
    
                if (srr[j] > ave)
                {
    
                    aa += srr[j]+" ";
                    count1++;
                }
            }
            Console.WriteLine("{0} ",aa);
            Console.WriteLine(count1);

6. Enter some integers , Find the largest number , Secondary maximum , minimum value , Secondary minimum

int[] srr = new int[10];
            for (int a = 0; a < 10; a++)
            {
    
                srr[a] =int.Parse( Console.ReadLine());
            }
            int temp = 0;
            for (int i = 0; i < 10; i++)
            {
    
                for (int j = 0; j < 9-i; j++)
                {
    
                    if (srr[j] > srr[j + 1])
                    {
    
                        temp = srr[j];
                        srr[j] = srr[j + 1];
                        srr[j + 1] = temp;
                    }
                   
                }
            }

            Console.WriteLine("{0},{1},{2},{3}",srr[9],srr[8],srr[0],srr[1]);

 Insert picture description here
7. Enter several ordered positive integers , For the same data, only one , Output retained data . for example , The input data is : 2,2,2,3,3,4,5,5,6,6,8,8,9,9,9,10,10,10 The final output is : 2,3,4,5,6,8,9,10.

            string str1=Console.ReadLine();;
            string ret = "";
            for (int i = 0; i < str1.Length; i++) 
            {
    
                char ch = str1[i];

                if (ret.IndexOf(ch.ToString()) >= 0)
                 {
    
                    continue;
                }
                ret = ret + ch.ToString();
            }
            Console.WriteLine(ret);
            Console.ReadLine();

 Insert picture description here
8. Enter a string , Judge if it's all numbers , Convert it to an integer , If other symbols are included , Give an error .

 string aa=Console.ReadLine();
            for (int i = 0; i < aa.Length; i++)
            {
    
                if (!char.IsDigit(aa[i]))
                {
    
                    Console.WriteLine("error");
                    break;
                }
                else
                    Console.WriteLine(aa);
                break;
            }

 Insert picture description here
9. Input 20 A positive integer , Count and output the number of odd and even numbers , And classify and output all odd and even numbers .

 int[] arr = new int[10] {
    1,2,3,4,5,6,7,8,9,10 };
            string aa="";
            string bb="";
            int count1 = 1;
            int count2 = 1;
            for (int i = 0; i < 10; i++)
            {
    
                if (arr[i] % 2 == 0)
                {
    
                    aa += arr[i]+" ";
                    count1++;
                }
                else
                {
    
                    bb += arr[i]+" ";
                    count2++;
                }
            }
            Console.WriteLine(aa);
            Console.WriteLine(count1);
            Console.WriteLine(bb);
            Console.WriteLine(count2);

 Insert picture description here
10. Input from terminal 5 Number , Output from small to large .

 int[] a = new int[6];
            Console.WriteLine(" Please enter six numbers :");
            for (int l = 0; l < 6; l++)
            {
    
                a[l] = int.Parse(Console.ReadLine());
            }
            for (int j = 0; j < 6; j++)
            {
    
                for (int i = 0; i<5; i++)
                {
    
                    int temp;
                    if (a[i] > a[i+1])
                    {
    
                        temp = a[i];
                        a[i] = a[i+1];
                        a[i+1] = temp;
                    }
                }
            }
            for (int k = 0; k < 6; k++)
            {
    
                Console.Write(" " + a[k]);
            }
            using System.Collections;
            ArrayList list = new ArrayList();
            for (int i = 0; i < 5; i++){
    
                list.Add(int.Parse(Console.ReadLine()));
            }
            list.Sort();
            for (int i = 0; i < list.Count; i++) {
     
                Console.WriteLine(list[i]);
            }

 Insert picture description here
11. Read in... From the keyboard 20 Data into array , Count the number of negative numbers , And calculate the sum of these negative numbers .

   int[] a = new int[20];
            Console.WriteLine(" Please enter twenty numbers :");
            for (int i = 0; i < 20; i++)
            {
    
                a[i] = int.Parse(Console.ReadLine());
            }

            int count = 0;
            double sum = 0;
            for (int i = 0; i < 20; i++)
            {
    

                if (a[i] < 0)
                {
    
                    count++;
                    sum += a[i];
                }
            }
            Console.WriteLine(" Number :{0} Negative numbers and :{1}", count, sum);

 Insert picture description here
12. seek n within ( barring n) Can't be 2 and 5 to be divisible by ( Can be 2 perhaps 5 Divisible but not divisible at the same time ) The square root of the sum of all natural numbers s,n Input from keyboard .

Console.WriteLine(" Please enter n");
            int n = int.Parse(Console.ReadLine());
            int sum = 0;
            for(int i=0;i<n;i++)
            {
    
                if(i%2!=0&&i%5!=0)
                {
    
                    sum += i;
                }               
            }
            double s = Math.Sqrt(sum);
            Console.WriteLine(s);

 Insert picture description here

random Generate 10 Number , Sort from small to large , Output max min , And the rest , Find the sum of the remaining numbers , Average .

 public static void avee()
        {
    
            int[] srr = new int[10];

            Random shuzu = new Random();
           
            for (int i = 0; i < 10; i++)
            {
    
                
                srr[i]= shuzu.Next(60, 100);
            }
            //for (int i = 0; i < 10; i++)
            //{
    
            // srr[i] =int.Parse( Console.ReadLine());
            //}
            int m;
            string ssss = "";
            double ave;
            double sum = 0;
            for (int n = 0; n < 10; n++)
            {
    
                for (int j = 0; j < 9 - n; j++)
                {
    
                    if (srr[j] > srr[j + 1])
                    {
    
                        m = srr[j];
                        srr[j] = srr[j + 1];
                        srr[j + 1] = m;
                    }
                }
            }
            for(int i=1;i<srr.Length-1;i++)
            {
    
                ssss += srr[i]+" ";
                sum += srr[i];
            }
            ave = sum / 8;
            Console.WriteLine(" The maximum value is {0}", srr[9]);
            Console.WriteLine(" The minimum value is {0}", srr[0]);
            Console.WriteLine(" and :{0} Average {1}",sum,ave);
            Console.WriteLine(ssss);
        }
 avee();
原网站

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