ざっくりと最適化しました。ちょっと自信あります。我ながらエレガントに書けたと思います。
//http://ja.doukaku.org/103/ 投稿用 using System; using System.Collections.Generic; class Program { static void Main(string[] args) { Amida(@"A B C D E
| - | |||
| - | - | ||
| - | - | ||
| - | - |
ざっくりと最適化しました。ちょっと自信あります。我ながらエレガントに書けたと思います。
//http://ja.doukaku.org/103/ 投稿用 using System; using System.Collections.Generic; class Program { static void Main(string[] args) { Amida(@"A B C D E
| - | |||
| - | - | ||
| - | - | ||
| - | - |
どう書く?orgのお題、あみだくじへの投稿用に書きました。もっとエレガントに書ける気がしますが、これで行きます。
//http://ja.doukaku.org/103/ 投稿用 using System; using System.Collections.Generic; class Program { static void Main(string[] args) { Amida(@"A B C D E
| - | |||
| - | - | ||
| - | - | ||
| - | - |
たまには自転車ネタでも書こうと思います。
車道に自転車レーンを設置する社会実験だそうです。これが定着すれば、日本での交通手段としての自転車の可能性はぐんと広がるでしょう。
ただ、レーンの幅が45センチというのは狭いです。歩道を走ることが出来る自転車の基準のひとつである普通自転車の車幅は、60センチ以下です。45センチだと大半の自転車はハミ出ます。きっと頭が弱い人が決めたのでしょう。普段から全く自転車に乗らない人でも簡単に分かることだと思います。
しかもこの自転車レーンが、道交法で言う自転車道と解釈されれば、幅45センチ以上60センチ以下の自転車は走るところが無くなります。
どちらにしろ、いくら道路に小細工しても人が変わらなきゃ意味がありません。一番大切で効果的なのは教育だと思うのですが、行政にその気は無いようです。
どう書く?orgのお題正整数のゲーデル数化?への投稿用に書きました。
既に投稿されているSiroKuroさんのコードのc – ‘0’ってところに萌えつつ、こういうときにIEnumeratorを使うと効率がいいのね~と勉強になりつつ、こんな感じで書いてみました。
分かりやすく書いたつもりがかえって意図が伝わりにくくなってる気がします。
参考としてリンクされいるWikiを見ても、ゲーデル数というものがが何なのかさっぱり理解出来ませんが、お題に示されているように書いてみました。
//http://ja.doukaku.org/100/ 投稿用 using System; using System.Collections.Generic; class Program { static void Main(string[] args) { Console.WriteLine(goedel(9)); Console.WriteLine(goedel(81)); Console.WriteLine(goedel(230)); Console.ReadLine(); } static double goedel(int n) { string tmpStr = n.ToString(); double r = 1; List<double> prime = new List<double>(new double[] { 2 }); for(int i = 3; ; i += 2) { bool isPrime = true; for(int j = 3; j < i; j++) { if(i % j == 0) { isPrime = false; break; } } if(isPrime) prime.Add(i); if(prime.Count >= tmpStr.Length) break; } for(int i = 0; i < tmpStr.Length; i++) { r *= Math.Pow(prime[i], (double.Parse(tmpStr[i].ToString()))); } return r; } }
C#|WebBrowserにマウスジェスチャー機能を簡単に追加のTextBox版を書きました。コンテキストメニューの表示設定の切り替えのタイミング以外は元のコードと殆ど同じです。このコンテキストメニューの表示設定の切り替えタイミングの試行錯誤に作業時間の殆どを費やしました。
これをしないと、マウスジェスチャが終わった後にメニューが開いちゃうんです。で、開かないようにしたら、二度と開かなかったりします。この微妙なタイミングが難しかったです。
なんでTextBox用にも書いたかと言うと、VB.NETで書いた電八用のビューアにマウスジェスチャ機能を付けたかったからです。↓で閉じる、これだけの為にです。
ここでVisualStudioだとVBのプロジェクトを参照設定してスタートアッププロジェクトに設定すれば楽なのでしょうが、僕が使っているのはC#、VBそれぞれのExpress Editionです。複数の言語を同時には扱えないので、C#でビルド→VBでテストという繰り返しでした。VSが欲しいです。MSはアカデミックパックに加えてNEETパックとかフリーターパックでも販売するべきです。
C#とVB.NETを交互に書くのは不思議な感じでした。
他の皆のコードよりかなり長かったので、短くしてみました。半分になりました。処理時間は倍になった気がします。
他の皆の他言語のコードも読めれば参考に出来るのですが、難しいです。
//http://ja.doukaku.org/ //http://ja.doukaku.org/99/投稿用 using System; using System.Collections.Generic; namespace どう書く_org文字列の八方向検索 { class Program { static string search; static List<string> list; static void Main(string[] args) { string sample = "リオウウリウ\nウオリウオリ\nオリリオリウ\nリリオオウオ"; search = "ウオリ"; char sp = new char { '\n' }; int width = sample.Split(sp)[0].Length; int height = sample.Split(sp).Length; list = new List<string>(sample.Split(sp)); for(int y = 0; y < list.Count; y++) { //縦ループ for(int x = 0; x < list[y].Length; x++) { //横ループ for(int dx = -1; dx <= 1; dx++) { //左右方向ループ for(int dy = -1; dy <= 1; dy++) { //上下方向ループ -1は上、左 1は下、右を表す try { string strb = ""; for(int i = 0; i < search.Length; i++) { //iは移動量 strb += list[y + i * dy][x + i * dx]; //移動方向をdy,dxで乗算することで反転 } if(search == strb) { string directionStr = ""; if(dx < 0) { directionStr += "左"; } else if(dx > 0) { directionStr += "右"; } if(dy < 0) { directionStr += "上"; } else if(dy > 0) { directionStr += "下"; } Console.WriteLine("(" + x + "," + y + ")" + "," + directionStr); } } catch(ArgumentOutOfRangeException) { } catch(IndexOutOfRangeException) { } } } } } Console.ReadLine(); } } }
お手製アドレス帳のCSVインポート機能のコードを書いていたのですが、
if(hoge.Text.Contains(“ZONU”)){
hoge.Text = hoge.Text.Replace(“ZONU”, “DQN”);
huga.Test = hoge.Text;
}
みたいなコードを書いて3行目のコードは動いて、2行目では置換されないってどういうことですか…?二日三日悩んでどうにもならなくてどうでも良くなりました。
Vectorでも漁ってればそのうち良いアドレス帳が見付かるでしょう。
どう書く?orgのお題、文字列の八方向検索投稿用に書きました。
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;
namespace どう書く_org文字列の八方向検索 {
class Program {
static string search;
static List<string> list;
static void Main(string[] args) {
string sample =
@"リオウウリウ
ウオリウオリ
オリリオリウ
リリオオウオ";
search = "ウオリ";
char sp = new char { '\n' };
int width = sample.Split(sp)[0].Length;
int height = sample.Split(sp).Length;
list = new List<string>(sample.Split(sp));
for(int y = 0; y < list.Count; y++) {
for(int x = 0; x < list[y].Length; x++) {
if(list[y][x] == search[0]) {
if(E(x, y) == search) {
Console.WriteLine("(" + x + "," + y + ")" + "," + "右");
}
if(W(x, y) == search) {
Console.WriteLine("(" + x + "," + y + ")" + "," + "左");
}
if(S(x, y) == search) {
Console.WriteLine("(" + x + "," + y + ")" + "," + "下");
}
if(N(x, y) == search) {
Console.WriteLine("(" + x + "," + y + ")" + "," + "上");
}
if(NE(x, y) == search) {
Console.WriteLine("(" + x + "," + y + ")" + "," + "右上");
}
if(SE(x, y) == search) {
Console.WriteLine("(" + x + "," + y + ")" + "," + "右下");
}
if(NW(x, y) == search) {
Console.WriteLine("(" + x + "," + y + ")" + "," + "左上");
}
if(SW(x, y) == search) {
Console.WriteLine("(" + x + "," + y + ")" + "," + "左下");
}
}
}
}
Console.ReadLine();
}
static string E(int x, int y) {
try {
return list[y].Substring(x, search.Length);
} catch(ArgumentOutOfRangeException) { return ""; }
catch(IndexOutOfRangeException) { return ""; }
}
static string W(int x, int y) {
try {
return Strings.StrReverse(E(x - search.Length + 1, y));
} catch(ArgumentOutOfRangeException) { return ""; }
catch(IndexOutOfRangeException) { return ""; }
}
static string S(int x, int y) {
try {
StringBuilder strb = new StringBuilder();
for(int i = 0; i < search.Length; i++) {
strb.Append(list[y + i][x]);
}
return strb.ToString();
} catch(ArgumentOutOfRangeException) { return ""; }
catch(IndexOutOfRangeException) { return ""; }
}
static string N(int x, int y) {
try {
return Strings.StrReverse(S(x, y - search.Length + 1));
} catch(ArgumentOutOfRangeException) { return ""; }
catch(IndexOutOfRangeException) { return ""; }
}
static string SE(int x, int y) {
try {
StringBuilder strb = new StringBuilder();
for(int i = 0; i < search.Length; i++) {
strb.Append(list[y + i][x + i]);
}
return strb.ToString();
} catch(ArgumentOutOfRangeException) { return ""; }
catch(IndexOutOfRangeException) { return ""; }
}
static string NW(int x, int y) {
try {
return Strings.StrReverse(SE(x - search.Length + 1, y - search.Length + 1));
} catch(ArgumentOutOfRangeException) { return ""; }
catch(IndexOutOfRangeException) { return ""; }
}
static string NE(int x, int y) {
try {
StringBuilder strb = new StringBuilder();
for(int i = 0; i < search.Length; i++) {
strb.Append(list[y - i][x + i]);
}
return strb.ToString();
} catch(ArgumentOutOfRangeException) { return ""; }
catch(IndexOutOfRangeException) { return ""; }
}
static string SW(int x, int y) {
try {
return Strings.StrReverse(NE(x - search.Length + 1, y + search.Length - 1));
} catch(ArgumentOutOfRangeException) { return ""; }
catch(IndexOutOfRangeException) { return ""; }
}
}
}
多言語度を上げる為にVB.NETに書き換えました。ついでに一番乗りです。
'http://ja.doukaku.org/ 'http://ja.doukaku.org/102/投稿用 Imports System Imports System.Windows.Forms NameSpace どう書く_orgフォルダパス一覧のツリー構造への変換 Class Program <STAThread> _ Shared Sub Main(byval args() As String) Application.Run(new Form1()) End Sub End Class class Form1:Inherits Form 'treeView1 Dim treeView1 As TreeView = New TreeView() '起動時引数でパス一覧のファイルを指定 Dim pathListFilePath As String = System.Environment.GetCommandLineArgs()(1) Public Sub New() 'treeView1 treeView1.Parent = Me treeView1.Dock = DockStyle.Fill End Sub Private Sub Form1_Load(byval sender As object, byval e As EventArgs)handles Me.Load 'ROOTNode Dim rootNode As TreeNode = New TreeNode("ROOT") treeView1.Nodes.Add(rootNode) For Each fullPath As String In System.IO.File.ReadAllLines(pathListFilePath) Dim addNode As TreeNode = rootNode For Each path As String In fullPath.Split(New char(){"\"c}) Dim flag As Boolean = True For Each node As TreeNode in addNode.Nodes If node.Text = path Then addNode = node flag = False End If Next if flag Then Dim newnode As TreeNode = New TreeNode(path) addNode.Nodes.Add(newnode) addNode = newnode End If Next Next rootNode.ExpandAll() End Sub End Class End NameSpace
どう書く?orgのトピックフォルダパス一覧のツリー構造への変換への投稿用に書きました。
//http://ja.doukaku.org/ //http://ja.doukaku.org/102/投稿用 using System; using System.Windows.Forms; namespace どう書く_orgフォルダパス一覧のツリー構造への変換 { class Program { [STAThread] static void Main(string[] args) { Application.Run(new Form1()); } } class Form1:Form { //treeView1 TreeView treeView1 = new TreeView(); //起動時引数でパス一覧のファイルを指定 string pathListFilePath = System.Environment.GetCommandLineArgs()[1]; public Form1() { //Form1 this.Load += new EventHandler(Form1_Load); //treeView1 treeView1.Parent = this; treeView1.Dock = DockStyle.Fill; } void Form1_Load(object sender, EventArgs e) { //ROOTNode TreeNode rootNode = new TreeNode("ROOT"); treeView1.Nodes.Add(rootNode); foreach(string fullPath in System.IO.File.ReadAllLines(pathListFilePath)) { TreeNode addNode = rootNode; foreach(string path in fullPath.Split(new char[] { '\\' })) { bool flag = true; foreach(TreeNode node in addNode.Nodes) { if(node.Text == path) { addNode = node; flag = false; } } if(flag) { TreeNode newnode = new TreeNode(path); addNode.Nodes.Add(newnode); addNode = newnode; } } } rootNode.ExpandAll(); } } }