Codewars风格排名系统【难度:4级】:
答案1:
using System;
public class User
{
private int _rank;
public int rank
{
get { return _rank; }
set { _rank = value == 0 ? 1 : value; }
}
private int _progress;
public int progress
{
get { return rank == 8 ? 0 : _progress; }
set
{
rank += value / 100;
_progress = value % 100;
}
}
public User()
{
rank = -8;
progress = 0;
}
public void incProgress(int level)
{
if (level < -8 || level > 8 || level == 0)
throw new ArgumentException();
int tempLevel = level > 0 ? level - 1 : level;
int tempRank = rank > 0 ? rank - 1 : rank;
int diff = tempLevel - tempRank;
progress += diff < -1 ? 0 : diff == -1 ? 1 : diff == 0 ? 3 : 10 *diff* diff;
}
}
答案2:
using System;
using System.Linq;
public class User
{
private static readonly int[] Ranks = new int[] { -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8 };
private int _rankIndex = 0;
public int progress { get; private set; }
public int rank => Ranks[_rankIndex];
public void incProgress(int activityRank)
{
if (!IsValidRank(activityRank)) throw new ArgumentException("Invalid activity rank");
if (rank == 8) return;
var activityRankIndex = Array.IndexOf(Ranks, activityRank);
var rankDifference = activityRankIndex - _rankIndex;
if (rankDifference < -2) return;
else if (rankDifference == -1) progress++;
else if (rankDifference == 0) progress += 3;
else progress += (10 *rankDifference* rankDifference);
UpdateRank();
}
private void UpdateRank()
{
if (progress >= 100)
{
var rankIncrease = progress / 100;
_rankIndex += rankIncrease;
var maxRankIndex = Ranks.Length - 1;
if (_rankIndex >= maxRankIndex) {
_rankIndex = maxRankIndex;
progress = 0;
} else {
progress %= 100;
}
}
}
private static bool IsValidRank(int inputRank) => Ranks.Contains(inputRank);
}
答案3:
using System;
public class User
{
private static int[] rankValues = new [] {-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8};
public int rank { get; set; } = -8;
public int progress { get; set; } = 0;
public void incProgress(int value)
{
if (value < -8 || value == 0 || value > 8)
throw new ArgumentException($"Activity with rank {value} is out of range");
if (rank == 8) return;
var rankDiff = Array.IndexOf(rankValues, value) - Array.IndexOf(rankValues, rank);
progress += rankDiff == 0 ? 3
: rankDiff == -1 ? 1
: rankDiff > 0 ? 10 *rankDiff* rankDiff
: 0;
while (progress >= 100 && rank < 9)
{
rank++;
progress-= 100;
if (rank == 0) rank++;
if (rank == 8) progress = 0;
}
}
}
答案4:
using System;
public class User
{
public int rank = -8;
public int progress = 0;
public void incProgress(int rank)
{
checkValidInput(rank);
incProgressValue(rank);
incRank();
}
private void checkValidInput(int input)
{
if (input > 8 || input < -8 || input == 0) throw new ArgumentException();
}
private void incProgressValue(int problemRank)
{
int d = calculateDistance(problemRank);
if(d == 0)
progress += 3;
else if (d == -1)
progress += 1;
else if (d > 0)
progress += 10 *d* d;
}
private int calculateDistance(int problemRank)
{
int d = problemRank - this.rank;
if(problemRank > 0 && this.rank < 0)
d--;
else if (problemRank < 0 && this.rank > 0)
d++;
return d;
}
private void incRank()
{
while (this.progress >= 100 && this.rank < 8)
{
if(this.rank == -1)
this.rank += 2;
else
this.rank += 1;
this.progress -= 100;
}
if(this.rank == 8) this.progress = 0;
}
}
答案5:
using System;
using System.Numerics;
public class User
{
string[] ranks = new string[] {"-8","-7","-6","-5","-4","-3","-2","-1","1","2","3","4","5","6","7","8"};
private int rankIndex;
public int progress;
public int rank;
public User(){
rank = -8;
rankIndex = 0;
progress = 0;
}
public void incProgress(int level){
if (Array.IndexOf(ranks, level.ToString()) < 0) {
throw new ArgumentException();
}
int diff = Array.IndexOf(ranks, level.ToString()) - rankIndex;
if(diff == 0){
progress += 3;
}
else if (diff < 0){
progress += 1;
}
else {
progress += (10 *diff* diff);
}
while(progress >= 100 && rank < 8){
rankIndex++;
rank = Int32.Parse(ranks[rankIndex]);
progress -= 100;
}
if(rank == 8){
progress = 0;
}
}
}
答案6:
using System.Collections.Generic;
using System.Linq;
public class User
{
public int rank;
public int progress;
public void incProgress(int parameter)
{
var RanksList = Enumerable.Range(-8, 17).ToList();
RanksList.RemoveAll(x => x == 0);
if (!RanksList.Contains(parameter) || parameter == 0)
{
throw new System.ArgumentException();
}
var difference = RanksList.IndexOf(parameter) -
RanksList.IndexOf(rank);
if (difference <= -2 || rank == 8)
return;
else if (difference == -1)
{
progress = progress + 1;
checkForRankUp(ref rank, ref progress);
}
else if (difference == 0)
{
progress = progress + 3;
checkForRankUp(ref rank, ref progress);
}
else if (difference >= 1)
{
progress += difference *difference* 10;
checkForRankUp(ref rank, ref progress);
}
else
{
return;
}
}
private void checkForRankUp(ref int rank, ref int progress)
{
while (progress > 100)
{
rank++;
progress -= 100;
if (rank == 0)
{
rank++;
}
}
if (rank >= 8)
{
rank = 8;
progress = 0;
}
}
public User()
{
rank = -8;
progress = 0;
}
}
答案7:
using System;
using System.Linq;
public class User
{
private readonly int[] Ranks = new[] { -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8 };
private int _currentRankIndex = 0;
public int rank
{
get
{
return Ranks[_currentRankIndex];
}
}
public int progress { get; internal set; }
public void incProgress(int inc)
{
if (Ranks.FirstOrDefault(x => x == inc) == 0)
{
throw new ArgumentException();
}
var newLevelPoints = 100;
var difference = Ranks.TakeWhile(x => x != rank).Count() - Ranks.TakeWhile(x => x != inc).Count();
var points = 10 *difference* difference;
if (difference == 0)
{
points = 3;
}
if (difference == 1)
{
points = 1;
}
var levels = (points + progress) / newLevelPoints;
var levelPoints = (points + progress) % newLevelPoints;
progress = levelPoints;
var newRankIndex = _currentRankIndex + levels;
if (newRankIndex > Ranks.Length)
{
throw new ArgumentException();
}
_currentRankIndex = newRankIndex;
if (rank == 8)
{
progress = 0;
}
}
}
答案8:
public class User
{
public User() {
rank = -8;
progress = 0;
}
public int rank
{
get; set;
}
public int progress
{
get; set;
}
public void incProgress(int i)
{
if (i >= -8 & i <= 8 && i != 0)
{
if (i == rank) progress += 3;
if (i == rank - 1||(i==-1&&rank==1)) progress += 1;
if (i >= rank + 1&&((i>0&&rank>0)||(i<0&&rank<0))) progress += 10 * (rank - i) * (rank - i);
else if(rank<0&&i>0) progress += 10 * (i-rank-1) * (i-rank-1);
rank += progress / 100;
if (rank == 0)
rank = 1;
if (rank >= 8)
{
progress = 0;
rank = 8;
}
else
progress = progress % 100;
}
else
throw new System.ArgumentException();
}
}
答案9:
using System;
using System.Collections.Generic;
public class User
{
List<int> ranks = new List<int>() {-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8};
public int rank, progress;
int rankIndex;
public User()
{
rankIndex = 0;
rank = ranks[rankIndex];
progress = 0;
}
public void incProgress(int activityRank)
{
if (!ranks.Contains(activityRank))
{
throw new System.ArgumentException("Rank is not in correct range", "activityRank");
}
int activityIndex = ranks.IndexOf(activityRank);
int diff = activityIndex - rankIndex;
if (diff > 0)
{
progress += 10 *diff* diff;
}
else if (diff == 0)
{
progress += 3;
}
else if (diff == -1)
{
progress += 1;
}
rankIndex += (int) (progress / 100);
progress %= 100;
if (rankIndex >= 15)
{
rankIndex = 15;
progress = 0;
}
rank = ranks[rankIndex];
}
}
答案10:
using System;
public class User {
public int rank;
public int progress;
public User() {
rank = -8;
progress = 0;
}
public void incProgress(int activityRank)
{
if(activityRank < -8 || activityRank == 0 || activityRank > 8)
{
throw new ArgumentException();
}
if (rank > 7 ) {
return;
}
if (activityRank == rank - 1 || activityRank == -1 && rank == 1)
{
progress++;
}
else if (activityRank == rank)
{
progress += 3;
}
else if (activityRank > rank)
{
int diff;
if (activityRank * rank < 0) {
diff = activityRank - (rank + 1);
}
else
{
diff = activityRank - rank;
}
progress += 10 *diff* diff;
}
while (progress >= 100)
{
progress -= 100;
if (rank == -1)
{
rank = 1;
}
else if(rank < 8)
{
rank++;
}
if(rank == 8)
{
progress = 0;
}
}
}
}