抽奖活动【难度:2级】:
答案1:
using System;
using System.Linq;
public class Rank
{
public static string NthRank(string st, int[] we, int n)
{
if (string.IsNullOrEmpty(st)) return "No participants";
var names = st.Split(new [] { ',', ' ' },
StringSplitOptions.RemoveEmptyEntries);
if (n > names.Length) return "Not enough participants";
var winner = names
.Select ((name, i) => new
{
Name = name,
WinningNumber = (name.Length +
name.ToLower().ToCharArray().Sum(c => (int)c)
- 96 * name.Length) * we[i]
}).OrderByDescending (item => item.WinningNumber)
.ThenBy (item => item.Name)
.ElementAt(n - 1).Name;
return winner;
}
}
答案2:
using System.Linq;
public class Rank
{
public static string NthRank(string st, int[] we, int n) =>
string.IsNullOrEmpty(st)
? "No participants"
: st.Split(',')
.Length < n ? "Not enough participants"
: st.Split(',')
.Select((s, i) => new {Name = s, Weigth = we[i]})
.OrderByDescending(s => s.Weigth * (s.Name.Length + s.Name.ToUpper().Sum(c => (int)c-64)))
.ThenBy(s => s.Name.ToUpper())
.ToList()
.ElementAt(n-1).Name;
}
答案3:
using System;
using System.Collections.Generic;
using System.Linq;
public class Rank
{
public static string NthRank(string st, int[] we, int n)
{
if (string.IsNullOrEmpty(st))
{
return "No participants";
}
var names = st.Split(',').ToList();
if (names.Count < n)
{
return "Not enough participants";
}
return names.Select(s => new KeyValuePair<string, int>(s, we[names.IndexOf(s)] * (s.Length + s.Sum(c => (int)c % 32)))).ToList().OrderBy(d => d.Key).OrderByDescending(d => d.Value).ElementAt(n - 1).Key;
}
}
答案4:
using System;
using System.Linq;
public class Rank
{
public static string NthRank(string st, int[] we, int n)
{
if (st == string.Empty)
{
return "No participants";
}
var names = st.Split(',');
if (names.Length < n)
{
return "Not enough participants";
}
var result = names.Select(x => x.ToLower().ToCharArray()
.Select(y => new { position = (int)y % 32 })
.ToList()
.Sum(p => p.position))
.Zip(names, (x, y) => new { name = y, value = x + y.Length })
.Zip(we, (x, y) => new { name = x.name, value = x.value * y })
.OrderByDescending(x => x.value)
.ThenBy(x => x.name)
.ElementAt(n - 1);
return result.name;
}
}
答案5:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Rank
{
public static string NthRank(string st, int[] we, int n)
{
if (st == String.Empty)
return "No participants";
List<Participiant> participiants=new List<Participiant>();
var f= st.Replace(" ",string.Empty).Split(',').ToList();
for (int i = 0; i < f.Count; i++)
{
participiants.Add(new Participiant(f[i],we[i]));
}
if (n > participiants.Count)
return "Not enough participants";
foreach (var a in participiants)
{
a.SetParticipiantValue();
}
var selection = participiants.OrderByDescending(p => p.Value).ThenBy(name=>name.Name).ToList();
return selection[n-1].Name;
}
}
public class Participiant
{
public Participiant(string name,int number)
{
this.Name = name;
this.Number = number;
}
public string Name { get; set; }
public int Number { get; set; }
public int Value { get;private set; }
public void SetParticipiantValue()
{
int sumName = Name.ToUpper().Sum(s => s-64);
Value = (sumName + Name.Length) * Number;
}
}
答案6:
using System;
using System.Linq;
public class Rank
{
private static string al = "_abcdefghijklmnopqrstuvwxyz";
public static string NthRank(string st, int[] we, int n)
=> ( string.IsNullOrEmpty(st) ) ? "No participants" :
( st.Count(e=> e == ',')< n-1 ) ? "Not enough participants" :
st.Split(',')
.Select((p,i)=> new { n=p, v= we[i]*( p.Length + p.Sum(x=> al.IndexOf(char.ToLower(x))))})
.OrderByDescending(f=> f.v).ThenBy(g=> g.n)
.Skip(n-1)
.First()
.n;
}
答案7:
using System;
using System.Collections.Generic;
using System.Linq;
public class Rank
{
public static string NthRank(string st, int[] we, int n)
{
if (st == ""){ return "No participants";}
if (st.Split(',').Length < n || we.Length < n){ return "Not enough participants";}
return st.Split(',').ToList().Zip(we, (one, two) => new {one, two}).ToDictionary(p => p.one, q => q.two)
.ToDictionary(p => p.Key, i => (i.Key.ToCharArray().Select(x => (int) x.ToString().ToLower().ToCharArray()[0] - 96)
.Sum(u => u) + i.Key.Length)*i.Value).OrderByDescending(p => p.Value).ThenBy(p => p.Key).ElementAt(n - 1).Key;
}
}
答案8:
using System.Linq;
public class Rank
{
public static string NthRank(string st, int[] we, int n)
{
if(string.IsNullOrEmpty(st))
{
return "No participants";
}
if(we.Length < n)
{
return "Not enough participants";
}
var names = st.Split(',');
var winnerList = we.Take(names.Length).Select((o,i) => new { wn = (names[i].Length + names[i].ToUpper().Select(c => c - 64).Sum()) * o, name = names[i] });
var winnerOrderedList = winnerList.OrderByDescending(o => o.wn).ThenBy(o => o.name).ToList();
return winnerOrderedList[n-1].name;
}
}
答案9:
using System;
using System.Linq;
public class Rank
{
public static string NthRank(string st, int[] we, int pos)
{
if (string.IsNullOrWhiteSpace(st)) return "No participants";
if (pos > we.Length) return "Not enough participants";
return st
.Split(',')
.Select((name, i) =>
{
return new
{
Rnk = (name.ToLower().Sum(n => (int)n - 96) + name.Length) * we[i],
Name = name
};
})
.OrderByDescending(n => n.Rnk)
.ThenBy(n => n.Name)
.ElementAt(pos-1)
.Name;
}
}
答案10:
using System.Linq;
public class Rank
{
public static string NthRank(string names, int[] ws, int n)
{
if (string.IsNullOrEmpty(names)) return "No participants";
string[] ns = names.Split(',');
if (ns.Length < n) return "Not enough participants";
return ns
.Select((name, i) => new { Name = name, Score = WinningNumber(name, ws[i]) })
.OrderByDescending(x => x.Score)
.ThenBy(x => x.Name)
.ElementAt(n - 1)
.Name;
}
private static int WinningNumber(string name, int w)
{
string n = name.ToLower();
return (n.Length + n.ToCharArray().Select(c => c - 'a' + 1).Sum()) * w;
}
}