得到的奇技淫巧

岁酱吖の 提交于 2019-11-27 18:56:03

1.read()快读函数

 

 1 //适用于正整数   2 template    3 inline void read(T &ret) {   4     char c; ret=0;   5     while((c=getchar())'9');   6     while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();   7 }   8 ////   9 //适用于正负整数  10 template   11 inline bool scan_d(T &ret) {  12    char c; int sgn;  13    if(c=getchar(),c==EOF) return 0; //EOF  14    while(c!='-'&&(c'9')) c=getchar();  15    sgn=(c=='-')?-1:1;  16    ret=(c=='-')?0:(c-'0');  17    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');  18    ret*=sgn;  19    return 1;  20 }  21 ////  22 //适用于正负数,(int,long long,float,double)  23 template   24 bool scan_d(T &ret){  25     char c; int sgn; T bit=0.1;  26     if(c=getchar(),c==EOF) return 0;  27     while(c!='-'&&c!='.'&&(c'9')) c=getchar();  28     sgn=(c=='-')?-1:1;  29     ret=(c=='-')?0:(c-'0');  30     while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');  31     if(c==' '||c=='\n'){ ret*=sgn; return 1; }  32     while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;  33     ret*=sgn;  34     return 1;  35 }
read

 

2.快写函数

 

1 //适用于正int  2 inline void out(int a)    3 {    4     if(a>=10)out(a/10);    5     putchar(a%10+'0');    6 }  

 

3.蔡勒公式(根据年月日求星期)

 

 1 int ReturnWeekDay( unsigned int iYear, unsigned int iMonth, unsigned int iDay )   2 {   3     int iWeek = 0;   4     unsigned int y = 0, c = 0, m = 0, d = 0;   5     if ( iMonth == 1 || iMonth == 2 )   6     {   7         c = ( iYear - 1 ) / 100;   8         y = ( iYear - 1 ) % 100;   9         m = iMonth + 12;  10         d = iDay;  11     }  12     else  13     {  14         c = iYear / 100;  15         y = iYear % 100;  16         m = iMonth;  17         d = iDay;  18     }  19     iWeek = y + y / 4 + c / 4 - 2 * c + 26 * ( m + 1 ) / 10 + d - 1;    //蔡勒公式  20     iWeek = iWeek >= 0 ? ( iWeek % 7 ) : ( iWeek % 7 + 7 );    //iWeek为负时取模  21     if ( iWeek == 0 )    //星期日不作为一周的第一天  22     {  23         iWeek = 7;  24     }  25     return iWeek;  26 }
蔡勒公式

 

4.全排列函数***(关于全排列的具体实现同sort()快排函数一样希望可以自己独立码出)***

next_permutation(start,end)和prev_permutation(start,end)

这两个函数区别在于前者求的是当前排列的下一个排列,而后者求的是当前排列的上一个排列
这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后
严格来讲,就是对于当前序列pn,他的下一个序列pn+1满足:不存在另外的序列pm,使pn<pm<pn+1.

对于next_permutation函数,其函数原型为:

 #include    bool next_permutation(iterator start,iterator end)

当目前序列不存在下一个排列时,函数返回false,否则返回true

栗子:

int main()  {      int num[3]={1,2,3};      do      {          cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;      }while(next_permutation(num,num+3));      return 0;  }

由此可以看出
next_permutation(num,num+n)函数是对数组num中的前n个元素进行全排列,同时并改变num数组的值

另::prex_permutation()应对的情况为{3,2,1}之类降序。

5.求字符串所有子串

1 void sub(string str) {  2     for (int i = 0; i < str.size(); i++)  3         for (int j = 1; j <= ((str.substr(i)).size()); j++)  4             cout << str.substr(i, j) << endl;  5 }

现在才仔细看原来是用了字符串类的方法。。。看样子这个类我了解的还是太少了🙈

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!