C#练习题答案: 帮助你的奶奶!【难度:3级】

编程入门 行业动态 更新时间:2024-10-26 08:23:31

C#<a href=https://www.elefans.com/category/jswz/34/1768594.html style=练习题答案: 帮助你的奶奶!【难度:3级】"/>

C#练习题答案: 帮助你的奶奶!【难度:3级】

帮助你的奶奶!【难度:3级】:

答案1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;public class Tour{public static int tour(string[] arrFriends, string[][] ftwns, Hashtable h){double prev = 0;return (int)(ftwns.Where(ftwn => arrFriends.ToList().Contains(ftwn[0])).Select(ftwn => prev == 0 ? prev = (double)h[ftwn[1]] : Math.Sqrt(-Math.Pow(prev, 2) + Math.Pow(prev = (double)h[ftwn[1]], 2))).Sum()+ (double)h[ftwns.Where(ftwn => arrFriends.ToList().Contains(ftwn[0])).Last()[1]]);}}​

答案2:

using System.Linq;
using System;
using System.Collections;public class Tour
{public static int tour(string[] f, string[][] t, Hashtable h){var cities = f.Select(e=> t.FirstOrDefault(x=> x[0]==e)).Where(y=> y!=null).Select(v=>(double) h[v[1]]);return (int)(cities.First() + cities.Last() +Enumerable.Range(1,cities.Count()-1).Select(i=> H(cities.Skip(i-1).First(), cities.Skip(i).First())).Sum());}    public static double H(double a, double b) => a < b? H(b,a) : Math.Sqrt( a*a-b*b);
}​

答案3:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
public class Tour{public static int tour(string[] arrFriends, string[][] ftwns, Hashtable h){if (arrFriends.Length<1)return 0;var dick = new Dictionary<string, string>();for (int i = 0; i < arrFriends.Length; i++){for (int j = 0; j < ftwns.Length; j++)if ((ftwns[j][0] == arrFriends[i]) &amp; (h.ContainsKey(ftwns[j][1])))if((double)(h[ftwns[j][1]])>0){dick.Add(arrFriends[i], ftwns[j][1]);break;}}if (dick.Count<1)return 0;double ans = (double)h[dick.Values.ElementAt(0)];for (int i = 0; i < dick.Count-1; i++){ans += Math.Sqrt(Math.Pow((double)h[dick.Values.ElementAt(i+1)], 2) - Math.Pow((double)h[dick.Values.ElementAt(i)], 2));}ans += (double)h[dick.Values.ElementAt(dick.Count-1)];return (int)Math.Floor(ans);}}​

答案4:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;public class Tour
{public static int tour(string[] arrFriends, string[][] ftwns, Hashtable h){var towns = new Dictionary<string, string>();ftwns.ToList().ForEach(x => towns.Add(x[0], x[1]));var route = new List<RoutePart>();arrFriends.ToList().ForEach(x => route.Add(new RoutePart(x, route, towns, h)));route.Add(new RoutePart(route, towns, h));return (int) Math.Floor(route.Sum(x => x.Distance));}public class RoutePart{public RoutePart (List<RoutePart> route, Dictionary<string, string> towns, Hashtable distances): this(null, route, towns, distances){}public RoutePart(string friend, List<RoutePart> route, Dictionary<string, string> towns, Hashtable distances) {Friend = friend;var lastPart = route.Where(x => !string.IsNullOrEmpty(x.End)).LastOrDefault();if (lastPart != null){Start = lastPart.End;}if (!string.IsNullOrEmpty(Friend)){if (towns.ContainsKey(Friend))End = towns[Friend];elsereturn;}Distance = GetDistance(distances);}public string Start {get; private set; }public string End {get; private set; }public string Friend {get; private set; }public double Distance { get; private set; }private double GetDistance(Hashtable distances){if (!string.IsNullOrEmpty(Start) &amp;&amp; !string.IsNullOrEmpty(End))return Math.Sqrt( Math.Pow((double)distances[End], 2)-Math.Pow((double)distances[Start], 2));if (!string.IsNullOrEmpty(Start))return (double)distances[Start];if (!string.IsNullOrEmpty(End))return (double)distances[End];return 0;}}}​

答案5:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;public class Tour{private static bool isIn(string str, string[] a){if (a[0].Equals(str)) return true; else return false;}private static List<string> doTour(string[] arrstr, string[][] friendtown){List<string> res = new List<string>{ "A0" };foreach (string str in arrstr){for (int i = 0; i < friendtown.GetLength(0); i++){if (isIn(str, friendtown[i])) res.Add(friendtown[i][1]);}}return res;}private static int distance(List<string> tour, Hashtable h){string x = tour[1];double sum = (double)h[x];for (int i = 1; i < tour.Count - 1; i++) {string x1 = tour[i];string x2 = tour[i + 1];double dx1 = (double)h[x1];double dx2 = (double)h[x2];double di = Math.Sqrt(dx2 * dx2 - dx1 * dx1);sum += di;}string xn = tour[tour.Count - 1];return (int)(sum + (double)h[xn]);}public static int tour(string[] arrFriends, string[][] ftwns, Hashtable h){List<string> r = doTour(arrFriends, ftwns);return distance(r, h);}}​

答案6:

using System;
using System.Collections;
using System.Linq;public class Tour
{public static int tour(string[] arrFriends, string[][] ftwns, Hashtable h){var dico = ftwns.ToDictionary(a => a[0], a => a[1]);var citys = arrFriends.Where(f => dico.ContainsKey(f)).Select(f => dico[f]).Distinct();var dists = citys.Select(c => (double)h[c]).ToList();var dists2 = dists.Zip(dists.Skip(1), (d1, d2)=>Math.Sqrt(d2*d2-d1*d1));return (int)(dists[0]+dists2.Sum()+dists[dists.Count-1]);}
}​

答案7:

using static System.Math;using System.Collections;
using System.Collections.Generic;
using System.Linq;public class Tour
{public static int tour(string[] friends, string[][] ftowns, Hashtable h){Dictionary<string, string> friendsTownsMap = ftowns.ToDictionary(k => k[0], v => v[1]);friends = friends.Where(e => friendsTownsMap.ContainsKey(e)).ToArray();double totalDistance = (double)h[friendsTownsMap[friends.First()]] + (double)h[friendsTownsMap[friends.Last()]];for(int i = 0; i < friends.Length - 1; i++){double dist1 = (double)h[friendsTownsMap[friends[i]]];double dist2 = (double)h[friendsTownsMap[friends[i + 1]]];totalDistance += Sqrt(dist2 * dist2 - dist1 * dist1);}return (int)totalDistance;}
}​

答案8:

using System;
using System.Collections;
using System.Collections.Generic;public class Tour{public class Stacja{public double lewo = 0;public double prawo = 0;public Stacja(double a, double b){lewo = a;prawo = b;}}public static int tour(string[] arrFriends, string[][] ftwns, Hashtable h){          List<Stacja> baza = new List<Stacja>();List<double> temp = new List<double>();List<string> ktosie = new List<string>();for(int i = 0; i < ftwns.Length; i++){temp.Add((double)h[ftwns[i][1]]);ktosie.Add(ftwns[i][1]);}for(int i = 0; i < temp.Count; i++){if (i == 0) baza.Add(new Stacja(0, Math.Sqrt(temp[i + 1] * temp[i + 1] - temp[i] * temp[i])));else if (i == temp.Count - 1) baza.Add(new Stacja(Math.Sqrt(temp[i] * temp[i] - temp[i - 1] * temp[i - 1]), 0));else baza.Add(new Stacja(Math.Sqrt(temp[i] * temp[i] - temp[i - 1] * temp[i - 1]), Math.Sqrt(temp[i + 1] * temp[i + 1] - temp[i] * temp[i])));}int indeks = 0;List<string> miasto = new List<string>();for(int i = 0; i < arrFriends.Length; i++){for(int j = 0; j < ftwns.Length; j++){if(arrFriends[i].Equals(ftwns[j][0])){miasto.Add(ftwns[j][1]);break;}}}for(int i = 0; i < miasto.Count; i++) if(ktosie[i].Equals(miasto[0])){indeks = i;break;}double res = temp[indeks];for(int i = 1; i < miasto.Count; i++){int indTp = 0;for (int j = 0; j < ktosie.Count; j++) if (miasto[i].Equals(ktosie[j])) { indTp = j; break; }if (Math.Abs(indeks - indTp) == 1){if (indTp > indeks) res += baza[indeks++].prawo;else res += baza[indeks--].lewo;}else{var k = Math.Sqrt(temp[indTp] * temp[indTp] - temp[indeks] * temp[indeks]);res += k;indeks = indTp;}}res += temp[indeks];return (int)Math.Floor(res);}}​

答案9:

using System.Collections;
using System;public class Tour{public static int tour(string[] arrFriends, string[][] ftwns, Hashtable h){double rd = 0, td, t = 0;for (int i = 0; i < arrFriends.Length; i++){for (int j = 0; j < ftwns.GetLongLength(0); j++){if (arrFriends[i] == ftwns[j][0]){IEnumerator ek = h.Keys.GetEnumerator();IEnumerator ev = h.Values.GetEnumerator();ek.Reset();ev.Reset();for (int hi = 0; hi < h.Count; hi++){ev.MoveNext();ek.MoveNext();string s = (string)ek.Current;if (s == ftwns[j][1]){td = (double)ev.Current;if (t == 0){t += td;rd = td;}else{t += Math.Sqrt(td * td - rd * rd);rd = td;}break;}}break;}}}t += rd;return (int)Math.Floor(t);}}​

答案10:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;public class Tour
{public static int tour(string[] arrFriends, string[][] ftwns, Hashtable h){string town= ftwns.Where( x=> x[0] == arrFriends[0]).Select( y=> y[1]).FirstOrDefault();double hypotenuse = Convert.ToDouble(h[town]);double total = hypotenuse;string finalKey ="";for ( int i=1; i<arrFriends.Length; i++){town = ftwns.Where( x => x[0]==arrFriends[i]).Select( y => y[1]).FirstOrDefault();if (town!= null &amp;&amp; h.ContainsKey(town)){total += Math.Sqrt(Math.Pow(Convert.ToDouble(h[town]), 2d)-Math.Pow(Convert.ToDouble(hypotenuse), 2d));hypotenuse= Convert.ToDouble(h[town]);finalKey = town;}else continue;}total+= Convert.ToDouble(h[finalKey]);return (int)total;}
}​



更多推荐

C#练习题答案: 帮助你的奶奶!【难度:3级】

本文发布于:2024-02-12 17:24:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1688713.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:练习题   难度   奶奶   答案

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!