c#编程题,这个难度普通,进链接或扫描看文本,会的留下代码带注释,感谢

链接: https://pan.baidu.com/s/19Vwl_t3CjFw3T1MwhD7fxQ

提取码: 4dmv

 

 

using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using static System.Math;
using System.Linq;
namespace ConsoleApp1
{
    public class City
    {
        public string city_ascii { get; set; }
        public string city_lowercase { get; set; }
        public double lat { get; set; }
        public double lng { get; set; }
        public double population { get; set; }
        public double distance { get; set; }

    }
    class Geo
    {
        const double RADIUS = 6378.16;

        public static double radians(double x) => x * PI / 180;

        public static double distance(double lat1, double long1, double lat2, double long2)
        {
            double dlat = radians(lat2 - lat1);
            double dlong = radians(long2 - long1);

            double a = Sin(dlat / 2) * Sin(dlat / 2) +
                       Cos(radians(lat1)) * Cos(radians(lat2)) * (Sin(dlong / 2) * Sin(dlong / 2));
            double angle = 2 * Atan2(Sqrt(a), Sqrt(1 - a));
            return angle * RADIUS;
        }
    }

    class Program
    {
        static void Main(string[] cmd)
        {
            var citys = File.ReadAllText("worldcities.csv", Encoding.UTF8).Split('\n').Skip(1);
            List<City> list = new List<City>();
            foreach (var r in citys)
            {
                if (r.Trim() == "") continue;
                Match m = Regex.Match(r, "\"([^\"]+)\",\"([^\"]+)\",(-?\\d+(\\.\\d+))?,(-?\\d+(\\.\\d+)?)((,\"[^\"]*\"){5}),(\\d*(\\.\\d+)?),(\\d+)", RegexOptions.Compiled);
                list.Add(new City
                {
                    city_ascii = m.Groups[2].Value,
                    city_lowercase = m.Groups[2].Value.ToLower(),
                    lat = double.Parse(m.Groups[3].Value),
                    lng = double.Parse(m.Groups[5].Value),
                    population = m.Groups[9].Value == "" ? 0 : double.Parse(m.Groups[9].Value)
                });
            }
            //var cmd = Console.ReadLine().Split(' ');
            cmd[0] = cmd[0].ToLower();
            var item = list.Where(i => i.city_lowercase == cmd[0]).FirstOrDefault();
            var value = double.Parse(cmd[2]);
            if (cmd[1] == "pop")
            {
                var rs = list.Where(i => i.population >= value && i.city_lowercase != cmd[0]);
                foreach (var r in rs)
                {
                    r.distance = Geo.distance(r.lat, r.lng, item.lat, item.lng);
                }
                Console.Write(String.Join("\n", rs.OrderBy(i => i.distance).Take(10).Select(i => i.city_ascii + ": pop = " + i.population.ToString("n0") + " / " + Math.Round(i.distance) + " km")));
            }
            else
            {
                foreach (var r in list)
                {
                    r.distance = Geo.distance(r.lat, r.lng, item.lat, item.lng);
                }
                Console.Write(String.Join("\n", list.Where(i=>i.distance<= value&&i.city_lowercase!=cmd[0]).OrderByDescending(i => i.population).Take(10).Select(i => i.city_ascii + ": pop = " + i.population.ToString("n0") + " / " + Math.Round(i.distance) + " km")));
            }

        }
    }
}

 

ted 1