ここでは配列をムダなく並び替える方法を提案します。
提案というより、いつもの「ループをまわして違う値が出るまで繰り返す」という方法をやめようという提案です。
何度もまわしていれば終わる作業ではあるはずですが、当然のごとくこのアルゴリズムだと終わるという確証がありません。
カウンタなどつけていれば無限ループという最悪の事態は防げるわけですが、ここでは「乱数は配列の大きさだけ計算すればいいはず」というアルゴリズムの提案です。
大げさなことはないわけなのですが、計算しなければならない乱数ってのは「まだ選択されていない数からのみ」なわけなので、その方法を示します。
まずはコード permute() をご覧ください。
… というわけです。選択されていない中から選ぶので乱数の計算が配列の大きさで済むというわけです。
どうでしょうか。
permute() の応用で、重複しない要素を選択する関数 withoutDuplicate(int n, int max) というのも作れます。
selectWithout(int n, int max, int excluded) は選択すべき要素から一定要素を除いた重複しない要素を選択する関数です。
よく使うアルゴリズムだと思います。
金曜日, 7月 03, 2015
火曜日, 6月 23, 2015
コマンドラインでシカゴの気温を調べる(C# で JSON データを読む)
先日 Java での JSON データの読み方を紹介しました。
ここでは C# で JSON データを読む方法を紹介します。
DataContractJsonSerializer を使った方法です。
DataContractJsonSerializer.ReadObject() を使うと JSON データを読み、オブジェクトとして出力してくれます。
オブジェクトへの変換が「宣言」となっているところがいくらか分かりやすいといえるでしょうか。
まず、読み込みたいデータ構造をクラスとして宣言し、DataContract 属性を付加します。JSON データは DataMember 属性を付加します。ネストされているデータはクラスとして宣言しDataContract 属性を付加します。コードを参照してください。#配列データは配列として宣言します。
出来上がったデータクラスを DataContractJsonSerializer のコンストラクタの引数として渡します。
準備が出来たらHttpWebResponse として読んできたデータを、DataContractJsonSerializer.ReadObject() で読み込みます。
と、この手順を踏むとめでたく JSON データが読み込めます。あとは出力するだけ。
ここの気温の表示はデフォルト設定でなんとK(ケルビン)です。ので 273.15 を引いてやります。
#華氏(℉)か摂氏(℃)か迷ったんでしょうか(もめたんですかね)。
ここでは C# で JSON データを読む方法を紹介します。
DataContractJsonSerializer を使った方法です。
DataContractJsonSerializer.ReadObject() を使うと JSON データを読み、オブジェクトとして出力してくれます。
オブジェクトへの変換が「宣言」となっているところがいくらか分かりやすいといえるでしょうか。
まず、読み込みたいデータ構造をクラスとして宣言し、DataContract 属性を付加します。JSON データは DataMember 属性を付加します。ネストされているデータはクラスとして宣言しDataContract 属性を付加します。コードを参照してください。#配列データは配列として宣言します。
出来上がったデータクラスを DataContractJsonSerializer のコンストラクタの引数として渡します。
準備が出来たらHttpWebResponse として読んできたデータを、DataContractJsonSerializer.ReadObject() で読み込みます。
と、この手順を踏むとめでたく JSON データが読み込めます。あとは出力するだけ。
ここの気温の表示はデフォルト設定でなんとK(ケルビン)です。ので 273.15 を引いてやります。
#華氏(℉)か摂氏(℃)か迷ったんでしょうか(もめたんですかね)。
火曜日, 8月 16, 2011
Why, just why?
The Professional edition of Visual Studio 2010 C++ does not compile dependent projects right.
It does not compile the static or dynamic libraries even when the configuration is set to compile them. This bug is more annoying since it says Rebuild All Succeeded when it is set to be compiled from the main project. This can be avoided if the project is compiled separately.
This is such a major bug that it is rather puzzling it is not set in that way, particularly in C++. Could there a way to compile them all together?
土曜日, 8月 13, 2011
Visual Studio: Refactoring
Microsoft's Visual Studio has a very nice feature called 'refactor'. It replaces the names of a method or function to whatever you rename it. It does contextual search and replace. The same name used in other namespace or scope will not be affected. It alleviates all the troubles associated with renaming. In the end, the code would come up closer to the final products no matter how you change your course of programming.
Visual Studio's C# refactor, however, is not perfect. It replaces the names even when there are the same names within the scope. There is no way to distinguish the variables that has renamed and the existing variables with the same name. That certainly is detectable. As for C++ version of the IDE, there isn't such a function to rename methods and variables with contextual searches.
Visual Studio's C# refactor, however, is not perfect. It replaces the names even when there are the same names within the scope. There is no way to distinguish the variables that has renamed and the existing variables with the same name. That certainly is detectable. As for C++ version of the IDE, there isn't such a function to rename methods and variables with contextual searches.
登録:
投稿 (Atom)
Flask の Blueprint のテンプレート問題
Flask の Blueprint は、ルート、静的ファイル、テンプレートをまとめて管理できます。しかし、テンプレートが指定できません。 ここでは、Blueprint の template_folder の問題点と回避策を説明します。 Blueprint のテンプレート問題...
-
コンデンサマイク 極性があります コンデンサマイクがようやく届きました。モジュールではないのでそのままでは信号が弱いので使えません。信号を増幅する必要があります。 ちょっとした手間ですが、首尾よく動いてくれました。この回路の応用で、使えるマイクが出来ます。 ...
-
Qt はまってます。Mac でも書いたアプリがコンパイルできて、しっかり動くのを確認しました。このあいだ Chitubox が Qt 使ってるのを知ってうれしかったです。 Mac でもそのまま動くんですが、アプリのアイコンの設定はプラットフォーム依存です。 公式マニュアルでも設定...
-
今日ホール素子が届きました。 ホール素子ってのは ホール効果 を利用して磁場を感知するセンサーです。 iPad のケースとか、閉じると電源が切れてくれますよね。ケースのカバーの磁石の磁場をホール素子が感知してるわけです。 さっそくホール素子を使ってみよう!電圧...
