複数のC++をコンパイルしたものをつなげる(例:a.out, b.out, ...)

3パターン書いてみた。

Pythonでつなげる

  • Pythonのsystemからa.outなどを実行して出力をファイルに書き、別の行でb.outを呼ぶ
  • 参考:「グルー言語」

windows batを書く

  • これもシンプル

Unix (Linux)のパイプを使う

  • 想定 a.cppとb.cppがあり、コンパイルしたものをa.out, b.outとする
  • a.outで何か標準出力をする
  • b.outで標準入力を受け取り、標準出力をする
// a.cpp
#include<bits/stdc++.h>
using namespace std;

int main(){
    cout << "I am a's output" << endl;
    return 0;
}
// b.cpp
#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    getline(cin, s);
    cout << "b got a message " << s << endl;
    return 0;
}
// コンパイル
g++ a.cpp -o a.out
g++ b.cpp -o b.out
// パイプでつなげる(aの出力をbの入力とする)
./a.out | ./b.out

// 出力(ターミナルの表示)はこうなる
b got a message I am a's output
  • 3つ目がテキスト書き出しをスキップできるので速度的には有利かな?

ブラウザの履歴を管理してください

f:id:peroon:20200612141658p:plain

  • というお題が出たとする
  • 戻る・進む・クリア・仕様変更に対応したい
  • シンプルにvectorを使うのがいいだろう
  • 今回確かめたかったのは、構造体を定義したとして、実際の変数を波括弧で作れるかどうか
  • 作れました。シンプルな書き方を知っておくのは良いこと
#include<bits/stdc++.h>
using namespace std;

struct History{
    int v;
    string url;
    string title;
    bool operator<(const History &another) const{
        return v < another.v;
    }
};

int main(){
    vector<History> H;
    H.push_back({2,"aaa","bbb"});
    H.push_back({1,"ccc","ddd"});
    H.push_back({4,"eee","fff"});
    H.push_back({3,"ggg","hhh"});

    sort(H.begin(), H.end());

    for(auto h : H){
        cout << h.v << ' ' << h.url << ' ' << h.title << endl;
    }

    return 0;
}

Output

1 ccc ddd
2 aaa bbb
3 ggg hhh
4 eee fff

判別式・2点を通る直線・2次方程式の解(x1,x2)

f:id:peroon:20200602002539p:plain

タイトルにある3つを関数化した

// 判別式
ll discriminant(ll a, ll b, ll c){
  return b*b - 4*a*c;
}

// 2次方程式の解(x1, x2)
pair<ld,ld> quadratic_equation(ll a, ll b, ll c){
  ll D = discriminant(a,b,c);
  ld x1 = (-b-sqrt(D))/(2*a);
  ld x2 = (-b+sqrt(D))/(2*a);
  return {x1,x2};
}

// 2点を通る直線を求める(係数 (a,b) を返す)
// y = ax + b
pair<ld,ld> line_from_two_point(ld x1, ld y1, ld x2, ld y2){
  if(x1==x2){
    // y軸と平行な直線
    cerr << "TODO"; exit(0);
  }
  if(y1==y2){
    // x軸と平行な直線
    return {0, y1};
  }
  // 傾き
  ld tilt = (y2-y1)/(x2-x1);
  ld a = tilt;
  ld b = a * (-x1) + y1;
  return {a,b};
}

verified

第二回 PAST H - 1-9 Grid ダイクストラ解法

Quiz

https://atcoder.jp/contests/past202004-open/tasks/past202004_h

AC code

https://atcoder.jp/contests/past202004-open/submissions/13146204

解法

  • 始点を0, 終点を10とします
  • 各グリッドの位置ごとにidを振っておきます
  • 数値 i -> 数値 i+1 のそれぞれのペアに、マンハッタン距離をコストとした辺をはります
  • SからEへのダイクストラすると答えが求まります

f:id:peroon:20200512172521p:plain