달력

42024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

C++ for ICPC

Contest 2014. 10. 21. 23:19
반응형

Read Until EOF


  while(getline(cin, data)) {
...
  }



Compare Strings


string a, b;

a.compare(b)



Set String To Upper


string word;

transform(word.begin(),word.end(),word.begin(),::toupper);



Append String


http://www.cplusplus.com/reference/string/string/append/

string (1)
string& append (const string& str);
substring (2)
string& append (const string& str, size_t subpos, size_t sublen);
c-string (3)
string& append (const char* s);
buffer (4)
string& append (const char* s, size_t n);
fill (5)
string& append (size_t n, char c);
range (6)
template <class InputIterator>
   string& append (InputIterator first, InputIterator last);
initializer list(7)
string& append (initializer_list<char> il);

Reverse String


string(word.rbegin(),word.rend())



When to use scanf or cin


small amount of input or cumbersome string input -> cin

big amount of input -> scanf



When to use printf or cout


small amount of output -> cout

big amount of output or printing double -> printf



Get Average

auto avg=accumulate(data.begin(),data.end(),0)/n;



Get Stdev

double avg;
double app(double x){return (x-avg)*(x-avg);}
void proc(){
  avg=data.sum()/n;
  double result=sqrt(data.apply(app).sum()/n);
  printf("%lf\n",result);
}


void proc(){
  ld avg=accumulate(data.begin(),data.end(),0.0)/n;
  transform(data.begin(),data.end(),data.begin(),[avg](ld x)->ld{return (x-avg)*(x-avg);});
  ld result=sqrt(accumulate(data.begin(),data.end(),0.0)/n);
  printf("%Lf\n",result);
}



Dice Library


struct Cube{
    /**
    *   0
    * 3 1 2 4
    *   5
    * <:y ^:x <>:z
    **/
    Cube(){}
    int d[6];
    void roll(int i,int j,int k,int l){
        int t=d[i];d[i]=d[j];d[j]=d[k];d[k]=d[l];d[l]=t;
    }
     
    void z(){roll(1,2,4,3);}
    void y(){roll(0,2,5,3);}
    void x(){roll(0,1,5,4);}
     
    void n(){x();}
    void s(){x(),x(),x();}
    void e(){y(),y(),y();}
    void w(){y();}
     
    int getRight(int top,int front){
        int right;
        for(int i=0;i<6;++i){
            for(int j=0;j<4;++j){
                if(d[0]==top&&d[1]==front)right=d[2];
                z();
            }
            if(i&1)x();
            else y();
        }
        return right;
    }
     
    bool isSame(Cube c){
        auto chk=[&]{for(int i=0;i<6;++i)if(d[i]!=c.d[i])return false;return true;};
        bool res=false;
        for(int i=0;i<6;++i){
            for(int j=0;j<4;++j){
                if(!res&&chk())res=true;
                z();
            }
            if(i&1)x();
            else y();
        }
        return res;
    }
};



Greatest Common Divisor


int gcd(int a,int b){
    do{
        int c=a%b;
        a=b;
        b=c;
    }while(b);
    return a;
}


Includes all of Standard Library Headers

#include <bits/stdc++.h> // g++ only





반응형
Posted by Taekhan
|