diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7456c47 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +JianZhiOffer/ +/**/a.out diff --git a/CoordiTransform.cpp b/CoordiTransform.cpp new file mode 100644 index 0000000..69b967a --- /dev/null +++ b/CoordiTransform.cpp @@ -0,0 +1,148 @@ +#include +#include +#include + +using namespace std; + +class Unit { +private: + using pos = unsigned int; + // using ctype = string; + pos rowIdx; + pos colIdx; + + //--- utilities ---// + // get row/col index from a R23C55 type + void getIdx_RC(string&); + // get index from a BC23 type + void getIdx_NRC(string&); + + // map from char to nums + char map[26] = {'A','B','C','D','E','F','G', + 'H','I','J','K','L','M','N', + 'O','P','Q','R','S','T', + 'U','V','W','X','Y','Z'}; + // return a num for the given char + pos alph2num(char _c) { + pos idx = 0; + for (; idx !=26; ++idx) { + if (map[idx] == _c) { + return idx + 1; + } + } + cerr << "letter not found!" << endl; + return 0; + } + + // return a char for the give num + char num2alph(pos _p) { return map[_p - 1]; } + + // return the corresponding string + string pos2letter(pos _p) { + if (_p <= 26){ + char c[1] = {num2alph(_p)}; + string s(c); + return s; + } + string res; + pos r = 0; + while (_p > 26) { + r = _p%26; + res += num2alph(r); + _p /= 26; + } + res += num2alph(_p); + std::reverse(res.begin(), res.end()); + // cout << "res: " << res << endl; + return res; + } + + // return the corresponding num + pos letter2pos(string& _s) { + auto len = _s.size(); + pos res = 0; + for (; len != 0; --len) { + res = res*26 + alph2num(_s[_s.size() - len]); + // cout << "res: " << res << endl; + } + return res; + } + +public: + // constructor1 + Unit(pos _r, pos _c) : rowIdx(_r), colIdx(_c) {} + + // constructor2 + Unit(string _a, bool _isRC) { + if (_isRC) { + getIdx_RC(_a); // R23C55 type + } else { + getIdx_NRC(_a); // BC23 type + } + } + + // selectors + pos getRow(void) { return rowIdx; } + pos getCol() { return colIdx; } + void convertor(bool); // coordinate convertor +}; + +// implemention +void Unit::getIdx_RC(string& _s) { + string::size_type c_pos = 0; + for (; c_pos!=_s.size(); ++c_pos) { + if (_s[c_pos] == 'C') break; + } + auto s1 = _s.substr(1, c_pos-1); + auto s2 = _s.substr(c_pos+1, _s.size()); + // cout << s1 << '\n' + // << s2 << endl; + rowIdx = std::stoi(s1); + colIdx = std::stoi(s2); +} + +void Unit::getIdx_NRC(string& _s) { + //rowIdx = std::stoi(_s); + string::size_type n_pos = 0; + for (; n_pos != _s.size(); ++n_pos) { + if (_s[n_pos] == '0' + || _s[n_pos] == '1' + || _s[n_pos] == '2' + || _s[n_pos] == '3' + || _s[n_pos] == '4' + || _s[n_pos] == '5' + || _s[n_pos] == '6' + || _s[n_pos] == '7' + || _s[n_pos] == '8' + || _s[n_pos] == '9' + ) break; + } + auto s1 = _s.substr(0, n_pos); + auto s2 = _s.substr(n_pos, _s.size()); + rowIdx = std::stoi(s2); + colIdx = letter2pos(s1); + // cout << "s1: " << s1 << '\n' + // << "s2: " << s2 << endl; +} + +// convertor +void Unit::convertor(bool _isRC) { + if (_isRC) { + cout << pos2letter(colIdx) + + std::to_string(rowIdx) << endl; + } else { + cout << "R" + + std::to_string(rowIdx) + + "C" + + std::to_string(colIdx) << endl; + } +} + +int main(){ + Unit u1(2, 3); + Unit u2("R23C55", true); + u2.convertor(1); + Unit u3("BC23", false); + u3.convertor(0); + return 0; +} diff --git a/NB.py b/NB.py new file mode 100644 index 0000000..7b40975 --- /dev/null +++ b/NB.py @@ -0,0 +1,16 @@ +import numpy as np + +def read_input(): + N = input() + N = int(N) + line = input().split() + Y = [int(_) for _ in line] + data = [] + + for _ in range(N): + line = input().split() + sz = line.size() + for x in line: + data.append(int(x)) + + mat = np.matrix(data, (N, sz)) diff --git a/Unit/Unit.h b/Unit/Unit.h new file mode 100644 index 0000000..0b278e6 --- /dev/null +++ b/Unit/Unit.h @@ -0,0 +1,33 @@ +#ifndef _UNIT_H_ +#define _UNIT_H_ + +#include + +class Unit { +private: + using pos = unsigned int; // type alias + pos rowIdx; + pos colIdx; + bool isRC; // is the type `R23C55` + void getIdx_RC(std::string); // build index for type 'R23C55' + void getIdx_NRC(std::string); // for type 'BC23' + +public: + // constructor1 + Unit(pos _r, pos _c) : rowIdx(_r), colIdx(_c) { } + // constructor2 + Unit(std::string); + + // selectors + pos getRow() { return rowIdx; } + pos getCol() { return colIdx; } + + // modifiers + void setRow(pos _r) { rowIdx = _r; } + void setCol(pos _c) { colIdx = _c; } + + // utilties + void printer(void); + void convertor(void); +}; +#endif diff --git a/Unit/Unit.hpp b/Unit/Unit.hpp new file mode 100644 index 0000000..e4e5d31 --- /dev/null +++ b/Unit/Unit.hpp @@ -0,0 +1,138 @@ +#include "Unit.h" +#include +#include + +// global map for quick search +char MAP[26] = {'A','B','C','D','E','F','G', + 'H','I','J','K','L','M','N', + 'O','P','Q','R','S','T', + 'U','V','W','X','Y','Z'}; + +// return a num for the given char +int alph2num(char _c) { + int idx = 0; + for (; idx != 26; ++idx) { + if (MAP[idx] == _c) + return idx + 1; + } + std::cerr << _c << "Not found!" << std::endl; + return 0; +} + +// return a char for the given num +char num2alph(int _i) { return MAP[_i - 1]; } + +void Unit::printer() { + std::cout << "row index: " << rowIdx + << "\ncol index: " << colIdx << std::endl; +} + +// declare function +bool typeInfer(std::string); +Unit::Unit(std::string _s) { + /* + * wishful thinking: i wish there is some function + * that helps me do the job + * e.g. + * - typeInfer() + * - getIdx_RC() + * - getIdx_NRC() + */ + isRC = typeInfer(_s); // set private isRC + if (isRC) { + getIdx_RC(_s); + } else { + getIdx_NRC(_s); + } +} + +bool typeInfer(std::string _s) { // infer the representation type + if (_s[0] == 'R' && std::isdigit(_s[1])) + return true; + else + return false; +} + +void Unit::getIdx_RC(std::string _s) { + /* + * build the row/col index for + * type 'R23C55' + */ + _s.erase(0, 1); // remove first 'R' + + // find where 'C' is + std::string::size_type c_pos = 0; + for (; c_pos != _s.size(); ++c_pos) { + if (_s[c_pos] == 'C') break; + } + auto s1 = _s.substr(0, 0 + c_pos); // s1 = "23" + auto s2 = _s.substr(c_pos + 1, _s.size()); // s2 = "55" + // set index + rowIdx = std::stoi(s1); + colIdx = std::stoi(s2); +} + +// how can I use Unit::pos here +int letter2pos(const std::string&); +void Unit::getIdx_NRC(std::string _s) { + /* + * build the row/col index for + * type 'BC23' + */ + + // find the first num + std::string::size_type n_pos = 0; + for (; n_pos != _s.size(); ++n_pos) { + if (std::isdigit(_s[n_pos])) + break; + } + + auto s1 = _s.substr(0, n_pos); // s1 should be "BC" + auto s2 = _s.substr(n_pos, _s.size()); // s2 should be "23" + rowIdx = std::stoi(s2); + colIdx = letter2pos(s1); +} + +int letter2pos(const std::string& _s) { + auto len = _s.size(); + int res = 0; + for (; len != 0; --len) { + res = res*26 + alph2num(_s[_s.size() - len]); + // std::cout << "res: " << res << std::endl; + } + return res; +} + + +// return the corresponding string +std::string pos2letter(int _p) { + if (_p <= 26){ + char c[1] = {num2alph(_p)}; + std::string s(c); + return s; + } + + std::string res; + int r = 0; + while (_p > 26) { + r = _p%26; + res += num2alph(r); + _p /= 26; + } + res += num2alph(_p); + std::reverse(res.begin(), res.end()); + // cout << "res: " << res << endl; + return res; +} + +void Unit::convertor() { + if (isRC) { + std::cout << pos2letter(colIdx) + + std::to_string(rowIdx) << std::endl; + } else { + std::cout << "R" + + std::to_string(rowIdx) + + "C" + + std::to_string(colIdx) << std::endl; + } +} diff --git a/Unit/test.cpp b/Unit/test.cpp new file mode 100644 index 0000000..9008534 --- /dev/null +++ b/Unit/test.cpp @@ -0,0 +1,20 @@ +#include "Unit.hpp" +#include + +using namespace std; + +int main() { + Unit u1(3, 4); + u1.printer(); + u1.setRow(33); + u1.setCol(44); + u1.printer(); + + Unit u2("R99C123"); + u2.printer(); + Unit u3("ABF417"); + u3.printer(); + u2.convertor(); + u3.convertor(); + return 0; +} diff --git a/a.out b/a.out deleted file mode 100755 index 12a7da6..0000000 Binary files a/a.out and /dev/null differ diff --git a/card.cpp b/card.cpp new file mode 100644 index 0000000..b451683 --- /dev/null +++ b/card.cpp @@ -0,0 +1,10 @@ +#include +#include +#include + +using namespace std; + +int opt() { + if (arr.size() == 2) return sum(arr); + +} diff --git a/cash.cpp b/cash.cpp new file mode 100644 index 0000000..17c425d --- /dev/null +++ b/cash.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +class Cash { +public: + int m; + int n; + vector koc; + + Cash(int _m, int _n) : m(_m), n(_n) { } + + int func(int m, int n) { + if (m == 0) return 1; + else if (m < 0 || n == -1) return 0; + else + return func(m, n-1) + + func(m - koc[n-1], n); + } +}; + + +int main(){ + int m, n; + cin >> m >> n; + + Cash cc(m, n); + + for (int i=0; i!=n; ++i) { + int tmp; + cin >> tmp; + cc.koc.push_back(tmp); + } + + cout << cc.func(m, n); + +} diff --git a/chuan.cpp b/chuan.cpp new file mode 100644 index 0000000..06ec6e9 --- /dev/null +++ b/chuan.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +void cancel(string& _s) { + if (_s.size() < 2) return; + int idx = 0; + for (; idx != _s.size()-1; ) { + if (_s[idx] != _s[++idx]) { + _s.erase(--idx, 2); + break; + } + } + cancel(_s); +} + +int main() { + int n = 0; + string str; + cin >> n; + cin >> str; + cancel(str); + cout << str.length(); + return 0; +} diff --git a/cxx b/cxx deleted file mode 100644 index 40831b1..0000000 Binary files a/cxx and /dev/null differ diff --git a/dancing.cpp b/dancing.cpp new file mode 100644 index 0000000..59d201e --- /dev/null +++ b/dancing.cpp @@ -0,0 +1,55 @@ +#include +#include + +using namespace std; + +int main() { + int n = 0, m = 0; + cin >> n >> m; + int t = m + n; // total number + vector > ans(t, vector(t, 0)); + + // build ans matrix + int x = 0; + for (int i = 0; i != n; ++i) { + int cnt = 0; + cin >> cnt; + for (int k = 0; k != cnt; ++k) { + cin >> x; + ans[i][n + x] = 1; + } + } + for (int i = n; i != t; ++i) { + int cnt = 0; + cin >> cnt; + for (int k = 0; k != cnt; ++k) { + cin >> x; + ans[i][x] = 1; + } + } + // End of building ans + + int songs = 0; + for (int i = 0; i != t; ++i) { + int temp = 0; + int up_line = 0; + int j = 0; + + if (i < n) { + j = n; + up_line = t; + } else { + j = 0; + up_line = n; + } + + for (; j != up_line; ++j) { + if (ans[i][j] == 1) ++temp; + else if (ans[j][i] == 1) ++temp; + } + + songs = songs < temp? temp : songs; + } + cout << songs << endl; + return 0; +} diff --git a/draft.cpp b/draft.cpp new file mode 100644 index 0000000..1e2b135 --- /dev/null +++ b/draft.cpp @@ -0,0 +1,67 @@ +// #include +// +// void fun(char* t, char* s) { +// while (*t != '\0') +// t++; +// while ((*t++=*s++) != '\0'); +// } +// +// int main() { +// char s1[10] = "123", s2[10] = "9978"; +// fun(s1, s2); +// printf(s1); +// return 0; +// } + +#include +#include +#include +#include + +using namespace std; + +class Solution {}; + +int numSee(const vector& soldierArray, int numSoldier) { + if (numSoldier == 0) return 0; + int curMax = soldierArray[0]; + int cnt = 1; + for (int i=1; i curMax) { + curMax = soldierArray[i]; + ++cnt; + } + } + return cnt; +} + +void f(double n) { + cout << "==>" << n << endl; +} +// entrance +int main(){ + /* + int numCols; + vector soldierArray; + vector res; + cout << "input numCols: \n"; + cin >> numCols; + for (int i=0; i!=numCols; ++i) { + int tmp = 0; + int numSoldier = 0; + cout << "input numSoldier: \n"; + cin >> numSoldier; + while (cin >> tmp) { + soldierArray.push_back(tmp); + } + res.push_back(numSee(soldierArray, numSoldier)); + } + + for (auto &c : res) { + cout << c << endl; + } + */ + double a = pow(2, 3); + f(a); + return 0; +} diff --git a/jd.cpp b/jd.cpp new file mode 100644 index 0000000..b4780de --- /dev/null +++ b/jd.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +using namespace std; + +int maxone(const string& s, + string::iterator beg, + string::iterator end) { + // boundary cases + if (beg >= end) return 0; + + auto pos1 = std::find(beg, end, "10"); + if (pos1 == end) + return static_cast(end - beg); + + auto pos2 = std::find(pos1, end, "01"); + if (pos2 == end) { + return static_cast(pos1 - beg); + } else { + ++pos2; + return max( + static_cast(pos1 - beg), + maxone(s, pos2, end) + ); + } +} + +int main() { + string s; + cin >> s; + while (s[0] == '1' && s.back() == '1') { + s.insert(0, "1"); + s.pop_back(); + } + int maxlen = maxone(s, s.begin(), s.end()); + cout << maxlen; + return 0; +} diff --git a/matdance.cpp b/matdance.cpp new file mode 100644 index 0000000..d6b1aca --- /dev/null +++ b/matdance.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +using namespace std; + + +class Pos { +public: + int x; + int y; +}; + +class Mat { +public: + vector ones; + vector twos; + vector > table; + + void read_input() { + int var; + while (cin >> var) { + table.push_back(vector()); + } + } + + int steps(int i, int j) { + int d_border = table.size() - 1; + int r_border = table[0].size() - 1; + + for (auto iter = twos.begin(); + iter != twos.end(); + ++iter) { + int i = iter -> x, + j = iter -> y; + build_IJ + + } + } + + void printer() { + for (auto &r : table) { + for (auto &c : r) + cout << c << ' '; + cout << endl; + } + } +}; + +int main() { + Mat m; + m.read_input(); + return 0; +} diff --git a/new_start/a.out b/new_start/a.out old mode 100644 new mode 100755 index 34db3b3..e84a1c6 Binary files a/new_start/a.out and b/new_start/a.out differ diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.1.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.1.cpp new file mode 100644 index 0000000..60a6739 --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.1.cpp @@ -0,0 +1,15 @@ +#include +#include + +using namespace std; + +istream& rw(istream& istrm) { + auto old_state = istrm.rdstate(); + istrm.clear(); // ready to use + char e; + while (istrm >> e) { + cout << e; + } + istrm.setstate(old_state); + return istrm; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.10.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.10.cpp new file mode 100644 index 0000000..fff4b2a --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.10.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + +using namespace std; + +// read a file by line, push them in a vector +vector read_file(string _fn) { + ifstream f(_fn); // open file + if(f) { // if stream is good + vector ret; + string line; + while (getline(f, line)) + ret.push_back(line); + + return ret; + } else { + cerr << "Cannnot open file: " + _fn + << endl; + return vector{"bad string"}; + } +} + +int main(int argc, const char* argv[]) { + vector svec = read_file(argv[1]); + string str; + + for (auto &e : svec) { + // read word using `stringstream` + istringstream iss(e); + while (iss >> str) { + cout << str << endl; + } + } + return 0; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.11.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.11.cpp new file mode 100644 index 0000000..83cc69d --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.11.cpp @@ -0,0 +1,63 @@ +#include +#include +#include + +using namespace std; + +struct PersonInfo { + string name; + vector phones; + + void printer() { + cout << "--> name: " << name << endl; + cout << "--> phones: "; + for (auto &e : phones) + cout << e << ' '; + cout << endl; + } +}; + +int main() { + string line, word; + vector people; + + /* + while (getline(cin ,line)) { + PersonInfo info; + istringstream record(line); + record >> info.name; + while (record >> word) + info.phones.push_back(word); + + people.push_back(info); + } + */ + + // if we put record outside + istringstream record; + while (getline(cin, line)) { + PersonInfo info; + record.str(line); // initialize stringstream + record >> info.name; + while (record >> word) + info.phones.push_back(word); + + people.push_back(info); + // cout< +#include +#include +#include + +using namespace std; + +struct PersonInfo { + string name; + vector phones; + + void printer() { + cout << "--> name: " << name << endl; + cout << "--> phones: "; + for (auto &e : phones) + cout << e << ' '; + cout << endl; + } +}; + +int main(int argc, const char* argv[]) { + vector people; + ifstream f(argv[1]); // open file + + if (f) { // stream `f` is good + string line, word; + while (getline(f, line)) { + PersonInfo info; + istringstream record(line); //bind to string stream + record >> info.name; + while (record >> word) + info.phones.push_back(word); + + people.push_back(info); + } + for (auto &e : people) + e.printer(); + } else + cerr << "Cannot open file: " + string(argv[1]) + << endl; + + return 0; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.2.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.2.cpp new file mode 100644 index 0000000..77d44ef --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.2.cpp @@ -0,0 +1,6 @@ +#include "cxxprimer-ex-8.1.cpp" + +int main(int argc, const char* argv[]) { + rw(cin); + return 0; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.3.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.3.cpp new file mode 100644 index 0000000..169ad80 --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.3.cpp @@ -0,0 +1,7 @@ +/* + * It will fail in one of the following: + * - strm.eof() + * - strm.fail() + * - strm.bad() + * or !strm.good() + */ diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.4.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.4.cpp new file mode 100644 index 0000000..a6378c3 --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.4.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using namespace std; + +vector read_file(string filename) { + // create stream and initialized + ifstream f(filename, ifstream::in); + vector ret; + string line; + + while (getline(f, line)) // read line + ret.push_back(line); + + f.close(); // close file + return ret; +} + +int main(int argc, char* argv[]) { + vector svec = read_file(argv[1]); + + for (auto &c : svec) { + cout << c << endl; + } + return 0; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.5.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.5.cpp new file mode 100644 index 0000000..f746ba9 --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.5.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using namespace std; + +vector read_file(string filename) { + // create stream and initialized + ifstream f(filename, ifstream::in); + vector ret; + string str; + + while (f >> str) // read word + ret.push_back(str); + + f.close(); // close file + return ret; +} + +int main(int argc, char* argv[]) { + vector svec = read_file(argv[1]); + + for (auto &c : svec) { + cout << c << endl; + } + return 0; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.6.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.6.cpp new file mode 100644 index 0000000..3d5c952 --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.6.cpp @@ -0,0 +1,26 @@ +#include "../Sales_data.ipp" +#include + +using namespace std; + +int main(int argc, char* argv[]) { + Sales_data total; + ifstream input(argv[1]); // open file + + if (read(input, total)) { + Sales_data trans; + while (read(input, trans)) { + if (total.isbn() == trans.isbn()) + total.combine(trans); + else { + // write to cout + print(cout, total) << endl; + total = trans; + } + } + print(cout, total) << endl; + } else + cerr << "No data?!" << endl; + + return 0; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.7.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.7.cpp new file mode 100644 index 0000000..19ad0e4 --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.7.cpp @@ -0,0 +1,27 @@ +#include "../Sales_data.ipp" +#include + +using namespace std; + +int main(int argc, char* argv[]) { + Sales_data total; + ifstream input(argv[1]); // open file + ofstream output(argv[2]); + + if (read(input, total)) { + Sales_data trans; + while (read(input, trans)) { + if (total.isbn() == trans.isbn()) + total.combine(trans); + else { + // write to local file + print(output, total) << endl; + total = trans; + } + } + print(output, total) << endl; + } else + cerr << "No data?!" << endl; + + return 0; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.8.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.8.cpp new file mode 100644 index 0000000..d7258ad --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.8.cpp @@ -0,0 +1,27 @@ +#include "../Sales_data.ipp" +#include + +using namespace std; + +int main(int argc, char* argv[]) { + Sales_data total; + ifstream input(argv[1]); // open file + ofstream output(argv[2], ofstream::app); // just one change here + + if (read(input, total)) { + Sales_data trans; + while (read(input, trans)) { + if (total.isbn() == trans.isbn()) + total.combine(trans); + else { + // write to local file + print(output, total) << endl; + total = trans; + } + } + print(output, total) << endl; + } else + cerr << "No data?!" << endl; + + return 0; +} diff --git a/new_start/cxxprimer/8/cxxprimer-ex-8.9.cpp b/new_start/cxxprimer/8/cxxprimer-ex-8.9.cpp new file mode 100644 index 0000000..dd14e6b --- /dev/null +++ b/new_start/cxxprimer/8/cxxprimer-ex-8.9.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + +istringstream& rw(istringstream& istrm) { + auto old_state = istrm.rdstate(); + // reset iostate will not affect + // the contents of a stream + istrm.clear(); + string e; + while (istrm >> e) + std::cout << e << endl; + + istrm.setstate(old_state); + return istrm; +} + +int main() { + istringstream is("you can do it!"); + rw(is); + return 0; +} diff --git a/new_start/PersonInfo.txt b/new_start/cxxprimer/PersonInfo.txt similarity index 100% rename from new_start/PersonInfo.txt rename to new_start/cxxprimer/PersonInfo.txt diff --git a/new_start/cxxprimer/Sales_data.h b/new_start/cxxprimer/Sales_data.h new file mode 100644 index 0000000..7c19878 --- /dev/null +++ b/new_start/cxxprimer/Sales_data.h @@ -0,0 +1,61 @@ +#ifndef _SALES_DATA_H_ +#define _SALES_DATA_H_ + +#include +#include + +class Sales_data { +private: + std::string bookNo; + unsigned units_sold = 0; + double revenue = 0.0; + +public: + // constructors + Sales_data() = default; + Sales_data(std::string a, unsigned b, double c) + : bookNo(a), units_sold(b), revenue(c) {} + + // cannot bind const to non-const + Sales_data(const Sales_data& a) + : bookNo(a.bookNo), + units_sold(a.units_sold), + revenue(a.revenue) {} + + Sales_data(std::string _bookNo) : Sales_data(_bookNo, 0, 0) {} + + // selectors + std::string isbn() const { return bookNo; } + unsigned get_sold() const { return units_sold; } + double get_revenue() const { return revenue; } + + // modifiers + Sales_data& set_bookNo(std::string a) { + bookNo = a; + return *this; + } + Sales_data& set_sold(unsigned a) { + units_sold = a; + return *this; + } + Sales_data& set_revenue(double a) { + revenue = a; + return *this; + } + + // sum datas for same books + Sales_data& combine(const Sales_data&); + double avg_price() const { + return units_sold? revenue / units_sold + : 0; + } +}; + +//--- non-interface ---// +// add two objects' sales' data +Sales_data add(const Sales_data&, const Sales_data&); +// read bookNo, units_sold, and price from a input stream +std::istream& read(std::istream&, Sales_data&); +std::ostream& print(std::ostream&, const Sales_data&); + +#endif // _SALES_DATA_H diff --git a/new_start/cxxprimer/Sales_data.ipp b/new_start/cxxprimer/Sales_data.ipp new file mode 100644 index 0000000..0c2f76f --- /dev/null +++ b/new_start/cxxprimer/Sales_data.ipp @@ -0,0 +1,40 @@ +/* + * Implemention of class `Sales_data` + */ + +#include "Sales_data.h" + +Sales_data& Sales_data::combine(const Sales_data& rhs) { + units_sold += rhs.units_sold; + revenue += rhs.revenue; + return *this; +} + +std::istream& read(std::istream& is, Sales_data& item) { + double price = 0.0; + unsigned sales = 0; + std::string book; + + is >> book >> sales >> price; +// std::cout << "what i read is: " +// << book << ' ' +// << sales << ' ' +// << price << std::endl; + item.set_bookNo(book). + set_sold(sales). + set_revenue(sales * price); + return is; +} + +std::ostream& print(std::ostream& os, const Sales_data& item) { + os << item.isbn() << ' ' + << item.get_sold() << ' ' + << item.get_revenue(); + return os; +} + +Sales_data add(const Sales_data& lhs, const Sales_data& rhs) { + Sales_data sum = lhs; // invoke copy constructor + sum.combine(rhs); + return sum; +} diff --git a/new_start/cxxprimer/haha.res b/new_start/cxxprimer/haha.res new file mode 100644 index 0000000..0df6122 --- /dev/null +++ b/new_start/cxxprimer/haha.res @@ -0,0 +1,6 @@ +Algorithm-Design 122 8043.2 +Block-Chain 62 4092 +Information-Theory 18 590.4 +Algorithm-Design 122 8043.2 +Block-Chain 62 4092 +Information-Theory 18 590.4 diff --git a/new_start/cxxprimer/sale.log b/new_start/cxxprimer/sale.log new file mode 100644 index 0000000..777e28f --- /dev/null +++ b/new_start/cxxprimer/sale.log @@ -0,0 +1,10 @@ +Algorithm-Design 19 70.0 +Algorithm-Design 19 70.0 +Algorithm-Design 20 68.8 +Algorithm-Design 20 68.8 +Algorithm-Design 22 59.8 +Algorithm-Design 22 59.8 +Block-Chain 31 66.0 +Block-Chain 31 66.0 +Information-Theory 9 32.8 +Information-Theory 9 32.8 diff --git a/new_start/tt.txt b/new_start/cxxprimer/tt.txt similarity index 100% rename from new_start/tt.txt rename to new_start/cxxprimer/tt.txt diff --git a/new_start/data_structures/arrayList/arrayList.tcc b/new_start/data_structures/arrayList/arrayList.tcc index 8d01d21..eea6ce6 100644 --- a/new_start/data_structures/arrayList/arrayList.tcc +++ b/new_start/data_structures/arrayList/arrayList.tcc @@ -1,5 +1,5 @@ /* - * the implementation of class arrayList + * The implemention of class arrayList */ #include "arrayList.h" diff --git a/new_start/ex8_11.cpp b/new_start/ex8_11.cpp index 9eb0604..a2d24b2 100644 --- a/new_start/ex8_11.cpp +++ b/new_start/ex8_11.cpp @@ -12,8 +12,8 @@ struct PersonInfo{ }; int main(int argc, char const *argv[]) { - string line,word; - std::vector people; + string line, word; + vector people; /*while(getline(cin,line)){ //book code PersonInfo info; diff --git a/new_start/run b/new_start/run deleted file mode 100644 index e427d2b..0000000 Binary files a/new_start/run and /dev/null differ diff --git a/numbers.cpp b/numbers.cpp new file mode 100644 index 0000000..d136ca1 --- /dev/null +++ b/numbers.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +using namespace std; + +// const protection since I will never change +// arr in the whole process. +int opt(const vector& arr, + int beg, int end, bool isFront) { + // base cases + if (beg >= end) return 0; + if (end - beg == 1) { + if (isFront) + return abs(arr[beg] - arr[beg-1]); + else + return abs(arr[beg] - arr[end]); + } + + // ELSE + int front_gain = 0, + back_gain = 0; + + if (isFront) { + front_gain = abs(arr[beg] - arr[beg-1]); + back_gain = abs(arr[end-1] - arr[beg-1]); + } else { + front_gain = abs(arr[beg] - arr[end]); + back_gain = abs(arr[end-1] - arr[end]); + } + return max( + front_gain + opt(arr, beg+1, end, true), + back_gain + opt(arr, beg, end-1, false) + ); +} + + +int main(){ + int N = 0; + cin >> N; + vector arr; + for (int i=0; i!=N; ++i) { + int tmp; + cin >> tmp; + arr.push_back(tmp); + } + // for (auto c : arr) cout << c << ' '; + int maxwin = max( + opt(arr, 1, arr.size(), true), + opt(arr, 0, arr.size()-1, false) + ); + cout << maxwin; + return 0; +} diff --git a/numsq.cpp b/numsq.cpp new file mode 100644 index 0000000..1cc5849 --- /dev/null +++ b/numsq.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int solver(int i, int j) { + if(i == j) return i+1; + else if (i < j) { + if (i+1 == j) { + return 2*(i+1); + } else { + return i+1 + solver(i, j-1); + } + } else { + if (j+1 == i) { + return 2*(j+1); + } else { + return j+1 + solver(i-1, j); + } + } +} + +int main(){ + int i = 0, j = 0; + cin >> i >> j; + cout << solver(i, j); +} diff --git a/pocker.cpp b/pocker.cpp new file mode 100644 index 0000000..1249f79 --- /dev/null +++ b/pocker.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +queue pile; + +void solution(int n) { + // initialize queue + for (int i=0; i != n; ++i) + pile.push(i + 1); + + while (!pile.empty()) { + cout << pile.front() << ' '; + pile.pop(); + pile.push(pile.front()); + pile.pop(); + } +} + +int main() { + int num_test = 0; + int n = 0; + cin >> num_test; + + while (num_test--){ + cin >> n; + solution(n); + cout << '\n'; + } + return 0; +} diff --git a/primality.cpp b/primality.cpp new file mode 100644 index 0000000..3d2d8da --- /dev/null +++ b/primality.cpp @@ -0,0 +1,43 @@ +//#include +#include +#include + +using namespace std; + +auto square = [](int x) -> int { return x*x; }; +auto myremainder = [](int x, int y) -> int { + if (y == 0) return -1; + return x%y; +}; + +int expmod(int base, int power, int m) { + if (power == 0) return 1; + else if (power&1) + return myremainder(base * expmod(base, power-1, m), + m); + else + return myremainder(square(expmod(base, power>>1, m)), + m); +} + +// Fermat test +bool fermat_test(int n) { + return + [n](int a) -> bool { + return a == expmod(a, n, n); + } (std::experimental::fundamentals_v2::randint(1, n-1)); +} + +// fast prime test +bool fast_prime(int n, int times) { + if (times == 0) return true; + else if (fermat_test(n)) + return fast_prime(n, times-1); + else + return false; +} + + +int main(){ + cout << fast_prime(19999, 10); +} diff --git a/run b/run deleted file mode 100755 index aba41d4..0000000 Binary files a/run and /dev/null differ diff --git a/spiralMatrix.cpp b/spiralMatrix.cpp new file mode 100644 index 0000000..00df45f --- /dev/null +++ b/spiralMatrix.cpp @@ -0,0 +1,116 @@ +/* + * Build a Spiral matrix in clockwise order + * Microsoft 2019 interview + */ + +#include +#include +#include // for using setw() + +using namespace std; + +class Mat { +private: + int n; // nxn matrix + vector > mat; + // is current coordinate a border? + bool isBorder(const int, + const int); + +public: + // constructor + Mat(int _n) { + n = _n; + while (_n--) { + // for a temp object, use `vector()` + mat.push_back(vector(n, 0)); + } + } + ~Mat(void) = default; + void buildMat(void); + void printer(void); +}; + +/* + * Basic idea: + * 1. populate the matrix until a border is reached + * 2. once reach a border, change directions, + * the directions is given by [0,1,2,3] which + * represent [right, down, left, up] + * 3. until n^2 elements are all populated + * + * Note: populate from large to small + */ + +void Mat::buildMat() { + int n2 = n*n; + int i = 0, + j = 0, + direction = 0; + + while (n2) { + direction %= 4; // --> [0,1,2,3] + while (!isBorder(i,j)) { + if (direction == 0) { + mat[i][j++] = n2--; + if (isBorder(i,j)) { + j--; + i++; + break; + } + } + if (direction == 1) { + mat[i++][j] = n2--; + if (isBorder(i,j)) { + i--; + j--; + break; + } + } + if (direction == 2) { + mat[i][j--] = n2--; + if (isBorder(i,j)) { + j++; + i--; + break; + } + } + if (direction == 3) { + mat[i--][j] = n2--; + if (isBorder(i,j)) { + i++; + j++; + break; + } + } + } + ++direction; // change the direction + } +} + +bool Mat::isBorder(const int _i, + const int _j) { + if (_i == n + || _i == -1 + || _j == n + || _j == -1 + || mat[_i][_j] != 0) return true; + return false; +} + +void Mat::printer() { + for (auto &row : mat) { + for (auto &col : row) { + cout << setw(3) << col << ' '; + } + cout << endl; + } +} + +// entrance +int main() { + Mat m(4); + m.buildMat(); + m.printer(); + return 0; +} diff --git a/zifu.cpp b/zifu.cpp new file mode 100644 index 0000000..2d2ce67 --- /dev/null +++ b/zifu.cpp @@ -0,0 +1,53 @@ +/* + * 百度2019笔试题 + * 用KMP算法求循环节 + */ +#include +#include +#include + +using namespace std; + +unsigned int calDiff(string _s) { + auto len = _s.length(); + if (len <= 1) return 0; + + auto str = _s; + vector known = {_s}; + unsigned int cnt = 0; + + while (--len) { + // construct new str + str.push_back(_s[0]); + str.erase(0,1); + //cout << "_s: " << _s + // << "\nstr: " << str << endl; + + bool isKnown = false; + // find if already known + for (auto &c : known) { + if (c == str) { + isKnown = true; + break; + } + } + if (!isKnown) { + ++cnt; + // update known list + known.push_back(str); + } + _s = str; + } + if (cnt < 1) { + return 0; + } else { + return cnt + 1; + } +} + +int main(){ + string s; + cin >> s; + cout << calDiff(s) << endl; + return 0; +}