Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Roman Numbers, Sorting through IComparer
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav // http://rextester.com/users/376 namespace Rextester { using RomanNumbersComparer=Rextester.Program; class Program:IComparer<string> { public static void Main(string[] args) { string[] romanNumbers={ "MCMXCIX", "MXCIX", "III", "II", "III", // repeated "I", "X", "V", "IV", "XCIX", "MXCIX", "VI", "IX", "MI", "I", // repeated "XXXI", "XXX", "LI", "VIII", "VII", }; // all strings must be VALID roman numbers RomanNumbersComparer rnc=new RomanNumbersComparer(); Array.Sort(romanNumbers,rnc); Array.ForEach(romanNumbers,s=>Console.WriteLine("{0}\t\t{1}",s,rnc.GetValueOf(s))); } public int Compare(string x,string y){return _valueOf(x).CompareTo(_valueOf(y));} public int GetValueOf(string x){return _valueOf(x);} Func<string,int> _valueOf=s=>{ Func<char,int> _valueOf=c=>{switch(c){case'I':return 1;case'V':return 5;case'X':return 10;case'L':return 50;case'C':return 100;case'D':return 500;case'M':return 1000;default:throw new Exception("Número Romano Inválido.");}}; int q=0,w=0,e=0,a=0; for(int i=0;i<s.Length;++i){ q=w;w=e;e=_valueOf(s[i]); if(w==0)continue; if(q==0){ if(w<e){a+=e-w;w=e=0;}else if(w>e){a+=w;w=0;} }else{ if(q==w&&w==e){a+=q+w+e;q=w=e=0;}else if(q==w){a+=q+w;q=w=0;}else{a+=q;} } } return a+q+w+e; }; } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,12 sec, absolute running time: 0,09 sec, cpu time: 0,08 sec, average memory usage: 17 Mb, average nr of threads: 3
edit mode
|
history
|
discussion
I 1 VII 7 VIII 8 XXX 30 XXXI 31 LI 51 MI 1001