標準ライブラリ一括インクルード
#include <bits/stdc++.h>
ABC050_A
文字列を分割して数値にする問題
#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> using namespace std; int stoi(std::string str){ int ret; std::stringstream ss; ss<<str; ss>>ret; return ret; } vector<string> split(const string &s, char delim) { /* 文字列分割してvector<string>を返す */ vector<string> elems; stringstream ss(s); string item; while (getline(ss, item, delim)) { if (!item.empty()) { elems.push_back(item); } } return elems; } int main() { string Str; vector<string> ans; int a,b; getline(cin, Str);//空白ごと1行入力 ans = split(Str,' '); a = stoi(ans[0]); b = stoi(ans[2]); if(ans[1]=="+") printf("%d\n",a+b); else printf("%d\n",a-b); return 0; }
空白を含めて一行入力したい場合は
getline(cin,str);
ABC051_A
文字列の置換
www.geocities.jp
#include<iostream> #include<string> using namespace std; // 文字列を置換する std::string Replace(std::string String, std::string From, std::string To ) { std::string::size_type Pos( String.find( From ) ); while( Pos != std::string::npos ) { String.replace( Pos, From.length(), To ); Pos = String.find( From, Pos + To.length() );//さっきまで探してた位置以降のFromを探す } return String; } int main() { string s; cin >> s; s = Replace(s,","," "); cout<<s<<endl; return 0; }
Newton法で平方根を求める
(define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (imporve guess x) x))) (define (improve guess x) (average guess (/ x guess))) (define (average x y) (/ (+ x y) 2)) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (sqrt x) (sqrt-iter 1.0 x))
C91の反省
C91の反省と忘備録
配布物
- 前日5日間くらいで仕上げたので完成度がかなり低かった
- 製本が上手く出来なかった(製本テープを使う,業者に依頼するなど必要)
- ゲームのクオリティが低い
- 自宅で印刷をする場合,かなりの時間と手間がかかる
設営
Python
Pythonでimport thisとやると下のような文が出ることが分かった.
今日から「Effective Python」を読み始める.
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
遺伝的アルゴリズムで画像再現
遺伝的アルゴリズムを用いて画像を再現するというプロジェクトを行った.
まだまだ検討箇所があるので現在の結果を貼っておく.
目標の画像
0世代
1000世代
ツイートは0~800世代くらい
https://twitter.com/vockyX/status/816646900392656897
今後の活動
・1000世代後に効果を発揮できるように評価関数を改良する
(1000世代くらいになると概形はほぼ完成しているので色を滑らかにするという目的に変える)
・低画質→高画質への変換器を作る
POJ1004 Financial Management
Global変数は0で初期化される.
scanf("")!=EOFと表現したい場合EOFマクロは<stdio.h>に入っているため<stdio.h>を除くとこの様に記述できない.
何も入力されない場合-1であることを利用(-1は全て1が立った状態)してbit反転の~で終了条件を作れる.
Project Euler 65 Convergents of e
projecteuler.net
e=[2;1,2,1,1,4,1,1,6,1,...,1,2k,1,...]
という連分数展開の問題
後ろから深さ優先探索で解く.
from fractions import Fravtions
すると分数型が出来て便利
gist.github.com
AOJ0087 Strange Mathematical Expression
逆ポーランド記法の問題
stackを用いる.
gist.github.com
C++で一行入力をするには
string s,tmp; getline(cin,s);//文字列sに一行入力 //cin.eof()空だったらtrue stringstream ss(s); while(ss >> tmp){ //空白で区切られたモノに分割する //一つの単位がtmpになる }
文字列をdouble型にする
atof(tmp.c_str());
tmpはstring型で直接関数atof()に渡せないので.c_str()メソッドを用いる.
atof()関数は文字列をdouble型にする.