obvious random number

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right

foreword

Keywords: primary algorithm, Huawei computer test

2023/02/11 Finished at midnight, let me optimize it tomorrow with a ready-made method

1. Topic

describe
Obviously, NN random integers between 1 and 500 are generated. Please delete the repeated numbers, that is, keep only one of the same numbers, remove the rest of the same numbers, and then sort these numbers from small to large, and output them in the order they are arranged.

Data range: 1 \le n \le 1000 \1≤n≤1000 , the size of the input number satisfies 1 \le val \le 500 \1≤val≤500
Enter a description:
The first line first inputs the number N of random integers. Enter an integer in each of the next N lines, representing a random number that is clearly generated. For the specific format, please refer to the "Example" below.
Output description:
Output multiple lines, representing the result of input data processing
Example 1
enter:
3
2
2
1
output:
1
2
Description: Enter an explanation:
The first number is 3, that is, N=3 of this small sample, indicating that 3 random integers between 1 and 500 are generated by computer, and then each line has a random number, a total of 3 lines, that is, these 3 random numbers are:
2
2
1
So the sample output is:
1
2

2. Original problem solving

The code is as follows (example):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MingMingNumber
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Sorting and deduplication by Luoluo
            // get n input numbers
            // This is the most primitive and primary, because the foundation is not very good, it is too complicated to understand
            // There is also a wonderful idea, space for time solution, write tomorrow
            string line;
            while ((line = System.Console.ReadLine()) != null)
            {
                // Note that while handles multiple case s
                int n = Convert.ToInt32(line);
                int[] arr = new int[n];
                for (int i = 0; i < n; i++)
                {
                    arr[i] = Convert.ToInt32(System.Console.ReadLine());
                }
                // First deduplicate and then sort
                // Deduplication idea: use len to record the length of the deduplication array, and use the new b array to access the deduplication array
                int len = 0;
                int[] arr1 = new int[n];//For the deduplicated array, the subscript len ​​is invalid
                // Loop end i=6,len=arr1.count()-1+1
                for (int i = 0; i < n; i++, len++)
                {
                    arr1[len] = arr[i];
                    for (int j = i + 1; j < n; j++)
                    {
                        if (arr[i] == arr[j])
                        {
                            len--;
                            break;
                        }
                    }
                }
                int[] arr2 = new int[len];//remove useless tail
                for (int i = 0; i < len; i++)
                {
                    arr2[i] = arr1[i];
                }
                System.Console.WriteLine("deduplication results");
                foreach (int i in arr2)
                {
                    System.Console.WriteLine(i);
                }
                // Bubble sort arr2
                for (int i = 0; i < len; i++)
                {
                    for (int j = i + 1; j < len; j++)
                    {
                        if (arr2[i] > arr2[j])
                        {
                            int temp = arr2[i];
                            arr2[i] = arr2[j];
                            arr2[j] = temp;
                        }
                    }
                }
                System.Console.WriteLine("Sort results");
                foreach (int i in arr2)
                {
                    System.Console.WriteLine(i);
                }
                //string[] tokens = line.Split();
                //System.Console.WriteLine(int.Parse(tokens[0]) + int.Parse(tokens[1]));
            }
        }
    }
}

Summarize

Use array deduplication and sorting, I use bubbling
Note: When judging whether a field is empty or Null in C#,
* We generally use the string.IsNullOrEmpty and string.IsNullOrWhiteSpace methods,
* The results of these two methods are consistent in most cases,
* But compared to the string.IsNullOrEmpty method,
* The string.IsNullOrWhiteSpace method also judges blank characters,
* For example, if a string is full of blank characters such as spaces,
* It is false under the judgment of string.IsNullOrEmpty,
* The string.IsNullOrWhiteSpace method is true.

Tags: Algorithm C#

Posted by software4 on Sat, 11 Feb 2023 07:01:04 +1030