「エラトステネスのふるい」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==エラトステネスのふるい== [VC++.NET]{{category ロジック}} using namespace System; static void screenNumber(array<bool>^ args) { args[0]…」) |
|||
1行目: | 1行目: | ||
==エラトステネスのふるい== | ==エラトステネスのふるい== | ||
− | [VC++.NET]{{category ロジック}} | + | [[VC++.NET]]{{category ロジック}} |
using namespace System; | using namespace System; | ||
− | static void screenNumber(array | + | static void screenNumber(array<bool>^ args) { |
args[0] = true; | args[0] = true; | ||
args[1] = true; | args[1] = true; | ||
− | for (int i=2; i | + | for (int i=2; i < args->Length; i++) { |
− | for (int j=2; j | + | for (int j=2; j < args->Length; j++) { |
int num = i * j; | int num = i * j; | ||
− | if (num | + | if (num >= args->Length) { |
break; | break; | ||
} | } | ||
20行目: | 20行目: | ||
} | } | ||
− | static void print(array | + | static void print(array<bool>^ ary) { |
int itemCnt = 0; | int itemCnt = 0; | ||
− | for (int i=0; i | + | for (int i=0; i < ary->Length; i++) { |
if (!ary[i]) { | if (!ary[i]) { | ||
Console::Write("{0,6:d} {1}", i, ((++itemCnt % 6 == 0)?"\n":"")); | Console::Write("{0,6:d} {1}", i, ((++itemCnt % 6 == 0)?"\n":"")); | ||
29行目: | 29行目: | ||
} | } | ||
− | int main(array | + | int main(array<System::String ^> ^args) |
{ | { | ||
int size = 0l; | int size = 0l; | ||
− | array | + | array<bool>^ ary = nullptr; |
try { | try { | ||
size = Int32::Parse(args[0]); | size = Int32::Parse(args[0]); | ||
− | ary = gcnew array | + | ary = gcnew array<bool>(size); |
} catch (Exception^ e) { | } catch (Exception^ e) { | ||
Console::WriteLine("引数が不正です\n{0}", e); | Console::WriteLine("引数が不正です\n{0}", e); |
2020年2月15日 (土) 07:59時点における版
エラトステネスのふるい
using namespace System; static void screenNumber(array<bool>^ args) { args[0] = true; args[1] = true; for (int i=2; i < args->Length; i++) { for (int j=2; j < args->Length; j++) { int num = i * j; if (num >= args->Length) { break; } args[num] = true; } } } static void print(array<bool>^ ary) { int itemCnt = 0; for (int i=0; i < ary->Length; i++) { if (!ary[i]) { Console::Write("{0,6:d} {1}", i, ((++itemCnt % 6 == 0)?"\n":"")); } } } int main(array<System::String ^> ^args) { int size = 0l; array<bool>^ ary = nullptr; try { size = Int32::Parse(args[0]); ary = gcnew array<bool>(size); } catch (Exception^ e) { Console::WriteLine("引数が不正です\n{0}", e); return -1; } screenNumber(ary); print(ary); return 0; }
© 2006 矢木浩人