2013-01-01から1年間の記事一覧

Project Euler 8

PE

Problem 8 char.IsDigit(c) で文字 c が数値かどうか判定 文字 c から数値への変換は、c - '0' で出来た using System; using System.IO; using System.Collections.Generic; namespace PE { public class PE008 { public static void Calc() { string text …

Project Euler 7

Problem 7 using System; using System.Collections.Generic; namespace PE { public class PE007 { public static void Calc() { var primes = new List<int>(); for (int i = 3; /* none */; i += 2) { bool isprime = true; foreach (int x in primes) { if (x</int>…

Project Euler 6

Problem 6 using System; namespace PE { public class PE006 { public static void Calc() { int sumsq = 0; for (int i = 1; i <= 100; i++) { sumsq += i * i; } int sqsum = (int)Math.Pow(100 * 101 / 2, 2); Console.WriteLine(sqsum - sumsq); } } }

Project Euler 5

PE

Problem 5 and と i のいずれでも割り切れる数は不要なので gcd で取り除く。lcm で求める方法もある。 using System; namespace PE { public class PE005 { public static void Calc() { int ans = 1; for (int i = 1; i <= 20; i++) { ans = ans / Util.Gc…

Project Euler 4

PE

Problem 4 using System; namespace PE { public class PE004 { public static bool IsPalindrome(int n) { string s = n.ToString(); for (int i = 0; i < s.Length/2; i++) { if (s[i] != s[s.Length - i - 1]) { return false; } } return true; } public…

Project Euler 3

PE

Problem 3 using System; using System.Collections.Generic; namespace PE { public class PE003 { public static void Calc() { long n = 600851475143; List<long> factors = new List<long>(); long x = 3; while (x*x <= n) { if (n % x == 0) { factors.Add(x); n </long></long>…

Project Euler 2

PE

Problem 2 using System; namespace PE { public class PE002 { public static void Calc() { const int n = 4000000; int a = 1; int b = 2; int sum = 2; while (b <= n) { int tmp = a + b; a = b; b = tmp; if (tmp % 2 == 0) sum += tmp; } Console.Wri…

varに戸惑う

C# の var の使いどころに戸惑っている。 これまで書いたことのある言語は、C++, Java, Python で、C# の var のような型推論は初めての体験。 練習のためにどう書くの問題を解いてみた。 第13回オフラインリアルタイムどう書くの参考問題。C#で解く。 - Qii…

Project Euler 1

PE

Problem 1 using System; namespace PE { public class PE001 { public static void Calc() { int sum = 0; for (int i = 3; i < 1000; i++) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } Console.WriteLine(sum); } } }

メソッド名の先頭は大文字にする

C#ではメソッド名の先頭は大文字にするようだ。そのうちに慣れるだろうか。

名前付き引数

using System; using System.Collections.Generic; namespace Example { class MainClass { public static void Foo(int a, int b) { Console.WriteLine("a:{0}, b:{1}", a, b); } public static void Main(string[] args) { Foo(a: 10, b: 30); Foo(b: 34, …

Fizz-Buzz問題

1から100までの数をプリントするプログラムを書け。ただし3の倍数のときは数の代わりに「Fizz」と、5の倍数のときは「Buzz」とプリントし、3と5両方の倍数の場合には「FizzBuzz」とプリントすること。 どうしてプログラマに・・・プログラムが書けないのか? using …

タプル

C#

using System; using System.Collections.Generic; namespace Example { class MainClass { public static void Main(string[] args) { var t = Tuple.Create(1, 0.4f, "Hello"); Console.WriteLine(t.Item1); Console.WriteLine(t.Item2); Console.WriteLin…

string.Join() の練習

C#

Xamarin Studio を使いながら、C# の勉強をしています。 using System; using System.Collections.Generic; namespace Example { class MainClass { public static void Main(string[] args) { int[] xs = { 3, -23, 4, 32 }; List<int> ys = new List<int>() { 889, 2</int></int>…

素数を求める(配列を使う練習)

C#

エラトステネスのふるいで、素数を求めます。配列を使う練習。 using System; namespace Example { class MainClass { public static void sieve(int n) { bool[] primes = new bool[n+1]; for (int i = 2; i <= n; i++) primes[i] = true; for (int i = 2; …

Xamarin Studio を使ってみる

C#

前回のハローワールドのプログラムは、Emacs を使用してプログラムを書きました。 IDE のコード補完を使いたいので、Xamarin Studio を使ってみることにしました。 Xamarin Stuio を開いたら、メニューの File -> New -> Solution.. を選択。New Solution と…

こんにちはC#

C#

C# を勉強します。 % cat Hello.cs using System; class Hello { public static void Main() { Console.WriteLine("Hello C#"); } } コンパイルと実行結果です。 % dmcs Hello.cs % mono ./Hello.exe Hello C#