Warning: unlink(/home/wao2023no7/code-chips.com/public_html/wp-content/lolipop-migrator//index.php): No such file or directory in /home/wao2023no7/code-chips.com/public_html/wp-content/plugins/lolipop-migrator/includes/class-lolipop-migrator-file.php on line 74
【解決方法】解決策を支援する | code chips

【解決方法】解決策を支援する

[ad_1]

At the start of class, I like to call roll. I like to go through my list of students in alphabetical order. Where possible, I like to call students by their first names. Of course, if two students have the same first name, I have to also give the last name so they know who I’m calling. Write a program to help me out. Given a class roll, it is going to tell how I should call the names.

Input
Input consists of up to 
 names, one per line, terminated by the end of file. Each line contains a first and a last name for a particular person. First and last names use 
 to 
 letters (a–z), always starting with an uppercase letters first followed by only lowercase letters. No two people will have exactly the same first and last names.

Output
Print the list of names, one per line, sorted by last name. If two or more people have the same last name, order these people by first name. Where the first name is unambiguous, just list the first name. If two people have the same first name, also list their last names to resolve the ambiguity.

Sample Input 1	
Will Smith
Agent Smith
Peter Pan
Micky Mouse
Minnie Mouse
Peter Gunn

Sample Output 1
Peter Gunn
Micky
Minnie
Peter Pan
Agent
Will

私が試したこと:

namespace ClassRoll
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>();

            // Read input names until end of file
            string input;
            while ((input = Console.ReadLine()) != "")
            {
                names.Add(input);
            }

            // Sort the names using a custom comparer
            names.Sort(new NameComparer());

            // Print the sorted names
            foreach (string name in names)
            {
                Console.WriteLine(name);
            }
        }
    }
    public class NameComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            // Split the names into first and last name
            string[] nameX = x.Split(' ');
            string[] nameY = y.Split(' ');

            string firstNameX = nameX[0];
            string firstNameY = nameY[0];

            string lastNameX = nameX[1];
            string lastNameY = nameY[1];

            // Compare last names
            int FirstNameComparison = firstNameX.CompareTo(firstNameY);
            if (FirstNameComparison != 0) //first names are the same
            {
                return FirstNameComparison;
            }

            //string firstNameX = nameX[0];
            //string firstNameY = nameY[0];

            // Compare first names if last names are the same
            return firstNameX.CompareTo(firstNameY);
        }
    }
}

[ad_2]

コメント

タイトルとURLをコピーしました