Algoritmos
Utilitários
Algoritmos para fazer conversões e alguns algoritmos pequenos e clássicos.
GCD e LCM
Algoritmo de Euclides para o GCD (máximo divisor comum) e LCM (mínimo múltiplo comum).
int gcd(int a, int b) { return !b ? a : gcd(b,a%b); } int lcm(int a, int b) { return (a*b)/gcd(a,b); } // CUIDADO COM OVERFLOW
IntToString
string IntToString(int n) { stringstream S; S << n; return S.str(); }
StringToInt
int StringToInt(string s) { stringstream S(s); int n; S >> n; return n; }
StringTokenizer
Tokeniza a string em pedaços separados por delimitadores.
vector<string> StringTokenizer(string s, string delim = "") { vector<string> res; int posString, posToken; posString = s.find_first_not_of(delim,0); while (posString >= 0) { posToken = s.find_first_of(delim,posString); if (posToken < 0) posToken = s.length(); res.push_back(s.substr(posString, posToken-posString)); posString = s.find_first_not_of (delim,posToken); } return res; }
ConvertToBase
// converts a number from base 10 to base B (B > 1) string ConvertToBase(int n, int b) { string s; while (n > 0) { s.push_back( (n%b < 10 ? '0' + n%b : 'A' + n%b - 10) ); n /= b; } reverse(s.begin(),s.end()); return s; }
page_revision: 3, last_edited: 1181823551|%e %b %Y, %H:%M %Z (%O ago)





