Skip to content

Instantly share code, notes, and snippets.

@pyetras
Created April 10, 2009 22:15
Show Gist options
  • Save pyetras/93330 to your computer and use it in GitHub Desktop.
Save pyetras/93330 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stack>
using namespace std;
stack<int> liczby;
int main (int argc, char const *argv[])
{
char c = 0;
while(true){
c = getchar();
if (c == '\n')
break;
else if (c >= '0' && c <= '9'){
liczby.push(c - '0');
}else{
int b = liczby.top();
liczby.pop();
int a = liczby.top();
liczby.pop();
switch(c){
case '+':
default:
a+=b;
break;
case '*':
a*=b;
break;
case '-':
a -= b;
break;
}
liczby.push(a);
}
}
cout << liczby.top() << endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <cassert>
using namespace std;
class compare{
public:
bool operator() (const string f, const string s) const{
int flen = f.length(), slen = s.length();
if (flen == slen){
return !(f >= s);
}else
return !(flen >= slen);
}
};
string dane1[1000000];
int znak(string s, int i){
return ((i>s.length())?0:s[s.length()-i]-'0');
}
void sortuj(int n, int len){
for (int k=1; k<=len; k++){
for(int i = 0; i<n; i++){
string s = dane1[i];
int zn = znak(s, k);
int j = i-1;
while(j >= 0 && znak(dane1[j], k) > zn){
dane1[j+1] = dane1[j];
j--;
}
dane1[j+1] = s;
}
}
}
compare komper;
void sortuj2(int n){
for(int i = 0; i<n; i++){
string s = dane1[i];
int j = i-1;
while(j >= 0 && komper(s, dane1[j])){
dane1[j+1] = dane1[j];
j--;
}
dane1[j+1] = s;
}
}
int main (int argc, char const *argv[])
{
ifstream file1("dane2003/liczby1.txt"), file2("dane2003/liczby2.txt");
assert(file1.is_open() && file2.is_open());
int mini = 1000000, syfry = 0, syfry8 = 0, syfry7 = 0, n=0, longest = 0;
while(!file1.eof()){
string a;
file1 >> a;
dane1[n] = a;
int len = a.length();
longest = max(len, longest);
if (len < 6){
stringstream liczba(a);
int l;
liczba >> l;
mini = min(mini, l);
}
bool has7 = false;
for (int j = len-1; j>=0; j--){
int i = a[j];
has7 = i=='7';
syfry8+= i == '8';
syfry++;
}
syfry7+=has7;
//if (has7) cout << a << endl;
n++;
}
cout << "Najmniejsza: " << mini << endl;
cout << "8: " << syfry8 << endl;
cout << "Bez 7: " << n - syfry7 << endl << endl;
//n = 100;
sortuj2(n);
/*for (int i =0; i < 100; i++)
cout << dane1[i] << endl;
//*/
cout << "1000.: " << dane1[1000] << endl;
cout << "1500.: " << dane1[1500] << endl;
cout << "2000.: " << dane1[2000] << endl << endl;
cout<<"Powtorzone: " << endl;
compare comp;
while (!file2.eof()){
string a;
file2 >> a;
int len = a.length();
for (int j = len-1; j>=0; j--){
syfry++;
}
if (*lower_bound(dane1, dane1 + n, a, comp) == a)
cout << a << endl;
}
cout << "Cyfr w plikach: " << syfry << endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <cassert>
#include <list>
using namespace std;
typedef list<int>::iterator iter;
int main (int argc, char const *argv[])
{
string plik = "dane2005/dane5-0.txt";
ifstream file;
for (char i = '1'; i <= '3'; i++){
plik[15] = i;
cout << plik << endl;
file.open(plik.c_str());
assert (file.is_open());
int maks = -999999, suma = 0, mini= 100000, maks2 = 0;
list<int> lista;
while(!file.eof()){
int a;
file >> a;
if (a >= 0){
suma += a;
} else {
maks = max(suma, maks);
if (a + suma > 0){
suma += a;
}else {
suma = 0;
}
}
lista.push_back(a);
mini = min(a, mini);
maks2 = max(maks2, a);
}
file.close();
//poniewaz wielkosci danych sa stosunkowo nieduze...
int n = maks2 - mini + 1;
int *dane = new int[n];
memset(dane, 0, sizeof(int)*n);
int wystapienia = 0, odp;
for(iter it = lista.begin(); it!=lista.end(); it++){
//zwiekszam wszystkie wartosci o |najmniejsza|, w ten sposob
//moge wrzucic do tablicy, bez ujemnych indeksow
dane[*it - mini] += 1;
if (dane[*it - mini] > wystapienia){
wystapienia = dane[*it - mini];
odp = *it;
}
}
cout << "suma: "<< maks << endl;
cout << "najczestszy: " << odp << endl;
cout<<endl;
delete[] dane;
}
return 0;
}
#define abs(x) ((x>=0)?x:-x)
#include <iostream>
using namespace std;
double g(double x){
return abs(1 + x*x/100 - x/200);
}
double f(double x){
return abs(-x*x/50);
}
int main (int argc, char const *argv[])
{
double a = f(0) + g(0), b;
double e = 0.1, S = 0;
for (double i = e; i<=10.0; i+=e){
b = g(i) + f(i);
S += (a+b)*e/2;
a = b;
}
cout << "S = "<< S << endl;
for (int i = 0; ;i+=1){
if((int)g(i) + (int)f(i) >= 26){
int f2 = -(int)f(i);
cout << "C = " << i+100 << endl;
printf("(%d, %d), (%d, %d), (%d, %d), (%d, %d)\n",
i, f2, i, f2+26, i+100, f2, i+100, f2+26);
break;
}
}
return 0;
}
#include <iostream>
#include <cassert>
#include <fstream>
#include <map>
using namespace std;
map<string, int> mapa;
typedef map<string, int>::iterator iter;
bool ispalindrom(string s, int len){
for (int i = 0; i<= len/2; i++){
if (s[i]!=s[len-1-i])
return false;
}
return true;
}
int main (int argc, char const *argv[])
{
ifstream file("dane2006/dane.txt");
assert(file.is_open() == true);
int wielokrotne = 0;
while (!file.eof()){
string s;
file >> uppercase >> s;
if (s.length()<2)
continue;
iter f = mapa.find(s);
if (f == mapa.end()){
mapa[s] = 1;
}else{
f->second += 1;
if (f->second == 2){
wielokrotne++;
}
}
}
cout << "wiecej niz raz: " << wielokrotne << endl;
int maks = 0, parzyste = 0, palindrom = 0;
string najczestszy;
for (iter it = mapa.begin(); it!= mapa.end(); it++){
if (it->second > maks){
maks = it->second;
najczestszy = it->first;
}
int len = it->first.length();
parzyste += (!((it->first[len-1] - 'A')%2))*it->second;
palindrom += ispalindrom(it->first, len)*it->second;
}
cout << "najczestsze: " << najczestszy << " (" << maks << " razy)" << endl;
cout << "parzyste: " << parzyste << endl;
cout << "palindrom: " << palindrom << endl;
return 0;
}
#include <iostream>
#include <list>
#include <math.h>
using namespace std;
typedef list<int>::iterator iter;
const int N = 100001;
bool sito[N];
list<int> primes;
bool prime(int a){
int sqr = sqrt(a) + 0.5;
for (int i = 2; i< sqr; i++){
if (a%i == 0){
return false;
}
}
return true;
}
bool superprime(int a){
int suma = 0;
while(a>0){
suma += a%10;
a/=10;
}
return sito[suma] == false;
}
bool superprimeb(int a){
int suma = 0;
while (a>0){
suma += a%2;
a/=2;
}
return sito[suma] == false;
}
void oblicz(int from, int to){
cout << '<' << from << "; " << to << '>' << endl;
int wynik = 0, iloscs = 0, sumab = 0;
for (int i = from; i<=to; i++){
if (!sito[i]){
if (superprime(i)){
iloscs++;
if (superprimeb(i)){
//cout << i << endl;
sumab += i;
wynik++;
}
}
}else if (superprime(i)){
iloscs++;
}
}
cout << "ilosc superb: " << wynik <<endl;
cout << "ilosc super: " << iloscs <<endl;
cout << "suma superb: " << sumab << " -> " << prime(sumab) << endl<<endl;
}
int main (int argc, char const *argv[])
{
sito[1] = true;
for (int i = 2; i<N; i++){
if (sito[i]) continue;
primes.push_back(i);
for (int j = 2; i*j<N; j++){
sito[i*j] = true;
}
}
oblicz(2, 1000);
oblicz(100, 10000);
oblicz(1000, 100000);
return 0;
}
#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;
string odwroc (string s, int od, int len){
string os;
for (int i = len -1; i>= od; i--)
os += s[i];
return os;
}
int main (int argc, char const *argv[])
{
ifstream file("dane2008/slowa.txt");
ofstream a("dane2008/hasla_a.txt");
ofstream b("dane2008/hasla_b.txt");
assert(file.is_open() && a.is_open() && b.is_open());
string longesta, shortesta;
int minia = 100, maksa = 0;
string longestb, shortestb;
int minib = 100, maksb = 0;
cout << "B o dl 12:\n";
while (!file.eof()){
string s;
file >> s;
if (s=="") continue;
int len = s.length();
string os = odwroc(s, 0, len);
if (len < minia){
shortesta = os;
minia = len;
}
if (len > maksa){
longesta = os;
maksa = len;
}
a << os <<endl;
//mozna lepiej (kmp)
for (int right = len -1; right >= 0; right--){
if (s[0] == s[right]){
int i, j;
for (i=1, j = right-1; i<j && s[i] == s[j]; i++, j--);
if (j<=i){
string wynik = odwroc(s, right+1, len) + s;
b << wynik << endl;
int len2 = len - right - 1 + len;
if (len2 == 12)
cout << wynik << endl;
if (len2 < minib){
shortestb = wynik;
minib = len2;
}
if (len2 > maksb){
longestb = wynik;
maksb = len2;
}
break;
}
}
}
}
cout << endl;
cout << "najdlusze a: " << longesta << " (" << maksa << ")\n";
cout << "najkrotsze a: " << shortesta << " (" << minia << ")\n\n";
cout << "najdlusze b: " << longestb << " (" << maksb << ")\n";
cout << "najkrotsze b: " << shortestb << " (" << minib << ")\n";
file.close();
a.close();
b.close();
return 0;
}
#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;
int euklid(int a, int b){
while (b){
int tmp = a%b;
a = b;
b = tmp;
}
return a;
}
bool rosnacy(int a){
int prev=a%10;
a/=10;
while (a){
if (a%10 >= prev)
return false;
prev = a%10;
a/=10;
}
return true;
}
int main (int argc, char const *argv[])
{
string nazwa = "dane2009/dane0.txt";
for (char c = '1'; c<='3'; c++){
nazwa[13] = c;
cout << nazwa << endl;
ifstream file(nazwa.c_str());
assert(file.is_open());
int a, nwd, suma = 0;
file >> a;
nwd = a;
while (!file.eof()){
file >> a;
nwd = euklid(nwd, a);
suma += rosnacy(a);
}
cout << "NWD: " << nwd << endl;
cout << "Rosnace: " << suma << endl << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment