Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
project euler 17, C#
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { int sum = 0; for(int i=1; i<1000; i++) sum += GetNumberInWords(i.ToString()).Replace(" ", "").Trim().Count(); Console.WriteLine(sum+"onethousand".Count()); } public static string GetNumberInWords(string n) { if (string.IsNullOrEmpty(n)) return ""; int number = Convert.ToInt32(n); if(number == 0) return "zero"; int type = 0; int remainder = number; string res = ""; if (number % 100 > 9 && number % 100 < 20) { res = GetPartInWords(number % 100, 1); type = 2; remainder = number / 100; } while (remainder != 0) { type++; int factor = 10; if(type >= 4) factor = 1000; res = GetPartInWords(remainder % factor, type).Trim() + " " + res.Trim(); remainder = remainder / factor; } string[] parts = res.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if(parts.Count() == 3) return string.Format("{0} {1} and {2}", parts[0], parts[1], parts[2]); if(parts.Count() == 4) return string.Format("{0} {1} and {2} {3}", parts[0], parts[1], parts[2], parts[3]); return res; } static string GetPartInWords(int number, int type) { if (type == 1) //0-19 { switch (number) { case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; case 10: return "ten"; case 11: return "eleven"; case 12: return "twelve"; case 13: return "thirteen"; case 14: return "fourteen"; case 15: return "fifteen"; case 16: return "sixteen"; case 17: return "seventeen"; case 18: return "eighteen"; case 19: return "nineteen"; } } if (type == 2) //20,30,40,50,60,70,80,90 { switch (number) { case 2: return "twenty"; case 3: return "thirty"; case 4: return "forty"; case 5: return "fifty"; case 6: return "sixty"; case 7: return "seventy"; case 8: return "eighty"; case 9: return "ninety"; } } if (type == 3) //100, 200, 300, 400, 500, 600, 700, 800, 900 { switch (number) { case 1: return "one hundred"; case 2: return "two hundred"; case 3: return "three hundred"; case 4: return "four hundred"; case 5: return "five hundred"; case 6: return "six hundred"; case 7: return "seven hundred"; case 8: return "eight hundred"; case 9: return "nine hundred"; } } return ""; } } }
run
|
edit
|
history
|
help
0
Hello-Heath-World
RRR
aaa
Math v9.1
Task List
ProbableCause
Expression Tree - Demo2
tesyyyyy
monopoly11
asdasd