2011-04-01から1ヶ月間の記事一覧

SRM167 div2 500

問題文 A collection of particles is contained in a linear chamber. They all have the same speed, but some are headed toward the right and others are headed toward the left. These particles can pass through each other without disturbing the…

日付関数

閏年か判定 bool isleap(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } 01:01:01とかになってたら、変換 sscanf(time, "%d:%d:%d", &hh, &mm, &ss); //string str = "01:01:01"とかなら、 for(int i = 0; i iss(str); iss >…

STL #define vector のまとめ。

元々の定義はこんな感じ. vector(); vector( const vector& c ); explicit vector( size_type num, const T& val = T() ); vector( input_iterator start, input_iterator end ); vector test(sz); //sz:要素数 vector test(sz, 0) //五個の要素を0で初期化…

オペレータ

class LessInt { public: bool operator()(const int& riLeft, const int& riRight) const { return riLeft ()); //と、あたかも、コンストラクタを呼び出しているがごとく見える. sort(a.begin(), a.end(), cmp) //

ポインタ(多次元配列)の復習

char *p[4] = {"hogehiu", "jijkan", "ahbcd", "oiutui"} //OK 要素数が4になっていさえすれば、後は自動的に空間を確保してくれる. char (*p)[4] = {"hiu", "abv", "bin", "kon", "ggg", "zzn", "con", "com"}; // string *str[4] = {"aaa", "bbb", "ccc",…

SRM161 div2 500

Problem Statement You will be given a vector cars containing a list of strings that will participate in the Train. The Train is a string that will be built using the given strings. The building process works as follows: Train is initialize…

scanf, printf

書式付出力(printf 系)変換文字列の書式は以下の形式である.printf % [フラグ][フィールド幅].[精度][h/l/L修飾][変換文字]scanfではこうなる。 % [代入抑止][フィールド幅][h/l/L修飾][変換文字] 例 sprintf(fl, %06f, (double)a); //1.2345 12.345 0034…

C言語的文字列操作の復習

#include 大文字,小文字 islower(ch); isupper(ch); 数字 isdigit(ch); int ->string型に. #include #include string IntToString(int number) { string stream ss; //ss(string型)なので、int型であるnumberで初期化できない×ss(number) //[ number; retur…

数系

double res; int t = (int)res; //切り下げ int m = (int)(res+0.5); //四捨五入 int r; if(res-t の ceil関数を使う. double ceil(double x) n-1 テスト用の最大値、最小値の算出 INF = 1 sqrt(A)をint型にしたいときの注意. もしかすると、sqrt(64) = 8.…

STLコンポーネント

コンテナ vectorとか 汎用アルゴリズム #include 反復子 iterator vector::iterator the_iterator; 関数オブジェクト #inculude アダプタ vector::reverse_iterator rit 割り当て子 allocator イテレータのカテゴリ内容提供元使える演算子 input_iterator(入…

listの個数の挙動

list list1(26, 'x'); cout while(!list1.empty()) { cout と、 cout for(int i = 0; i { cout は違うんだぜ. 二つ目のはsize()がループするごとにどんどん可変的に減っていくので、ちょうど半分で出力が止まってしまうのです.

SRM 168 DIV2 500

Problem Statement Select two numbers between 1 and 9998, inclusive, which have the same exact group of non-zero digits, but are not the same number. For example, you could use 1234 and 4321, or 91 and 901. Now, subtract the smaller of the …

動的な二次元配列の作り方

//100*10の行列を作る vector > P(100, vector(10, 0)); あとは同じ。

char, string型から、intへの変換 SRM 209 div2

01:22:09 とかのstring型が与えられた場合、int型に何らかの形で変換したい。 //仮引数 //vector test(10) //vector test(SIZE) vector ts //1つの配列をもつ、string型。 for(int i = 0; i sscanf(ts[i].c_str(), "%d:%d:%d", &h, &m, &s); ; //いろいろ処…

コンストラクタでのメンバ変数が配列だったときの初期化の方法.

今回迷ったコード #include using namespace std; class PartSum { public: PartSum(int* array, int count, int cond) : m_count(count) { //m_array = array とやってしまうと、arrayをいじくることにより、メンバ変数m_arrayも代わってしまうので、止めた…

リスト作成の際の注意

リスト作成して、ノードを挿入したい。 #include using namespace std; typedef struct _list { int data; struct _list *next; }MyList; class Node { public: Node(); ~Node(); private: MyList* m_head; public: void Insert(int num); void Disp(); }; N…

文字列の操作いろいろ

Boyer-Moore法 void table(char *key) { int len = strlen(key); fill(skip, skip+CHAR_SET, len); for(int i = 0; i len-1; i++) { skip[key[i]] = len-1-i; } } //位置取得。戻り値はインデックス int search(char* text, char* key) { int textlen = strl…

二分木の実装とか

ノードを構造体で表す typedef struct _node { int val; struct _node *lch; struct _node *rch; } node; 赤色注意。まだ、typedefしていないので、こう書かなければ、エラー 挿入 //x:挿入する数字 p:検索するノード node *insert(node *p, int x) { //ノ…

ソートの練習

直接選択法 int main() { int n; cin >> n; int *arr = new int[n]; for(int i = 0; i > arr[i]; } //ここから本題 for(int i = 0; i (arr, i, n); swap(arr[min], arr[i]); } for(int i = 0; i バブルソート int main() { int n; cin >> n; int *arr = new …

配列群 MyArray

namespace MyArray { //[begin, end)の範囲での配列の最小値のインデックスを求める template int ArrayMinIndex(TYPE* array, int begin, int end) { int min = begin; //return for(int i = begin+1; i array[i]) { min = i; } } return min; } //[begin, …

newとdelete 配列編

要素数のわからない配列を入力したいとき int main() { int n; cin >> n; int *arr = new int[n]; //int arr = new int[n];では失敗する。 for(int i = 0; i > arr[i]; } delete arr; }

参照とアドレスの実験

int main() { int a; a = 1; //なくても、エラーは出ない。 cout int main() { int &a; //参照が初期化されずに宣言されています。 cout int main() { int b = 5; int &a = b; //直接数値を代入するとエラー cout int main() { int b = 5; int &a = b; //箱…

テキストモードとバイナリモード

違いは、改行コードの取り扱いのみ。 テキストモードでは、違うOSでも改行をそれぞれ環境に合った形で読み換えてくれる。 Window "\r\n" MacOS "\r" 良く、バイナリモードの説明で #include #include #include using namespace std; int main() { fstream fi…