得到的奇技淫巧
作者:互联网
1.read()快读函数
```
//适用于正整数
template <class T>
inline void read(T &ret) {
char c; ret=0;
while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
}
////
//适用于正负整数
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if(c=getchar(),c==EOF) return 0; //EOF
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
////
//适用于正负数,(int,long long,float,double)
template <class T>
bool scan_d(T &ret){
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
```
2.快写函数
```
//适用于正int
inline void out(int a)
{
if(a>=10)out(a/10);
putchar(a%10+'0');
}
```
3.蔡勒公式(根据年月日求星期)
![图片说明](https://uploadfiles.nowcoder.com/images/20190810/308237136_1565366685040_7997DB02B3C3423D6712AC5847900212 "图片标题")
```
int ReturnWeekDay( unsigned int iYear, unsigned int iMonth, unsigned int iDay )
{
int iWeek = 0;
unsigned int y = 0, c = 0, m = 0, d = 0;
if ( iMonth == 1 || iMonth == 2 )
{
c = ( iYear - 1 ) / 100;
y = ( iYear - 1 ) % 100;
m = iMonth + 12;
d = iDay;
}
else
{
c = iYear / 100;
y = iYear % 100;
m = iMonth;
d = iDay;
}
iWeek = y + y / 4 + c / 4 - 2 * c + 26 * ( m + 1 ) / 10 + d - 1; //蔡勒公式
iWeek = iWeek >= 0 ? ( iWeek % 7 ) : ( iWeek % 7 + 7 ); //iWeek为负时取模
if ( iWeek == 0 ) //星期日不作为一周的第一天
{
iWeek = 7;
}
return iWeek;
}
```
4.全排列函数***(关于全排列的具体实现同sort()快排函数一样希望可以自己独立码出)***
next_permutation(start,end)和prev_permutation(start,end)
这两个函数区别在于前者求的是当前排列的下一个排列,而后者求的是当前排列的上一个排列
这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后
严格来讲,就是对于当前序列pn,他的下一个序列pn+1满足:不存在另外的序列pm,使pn<pm<pn+1.
对于next_permutation函数,其函数原型为:
#include <algorithm>
bool next_permutation(iterator start,iterator end)
当目前序列不存在下一个排列时,函数返回false,否则返回true
栗子:
```
#include <iostream>
#include <algorithm>
using namespace std;
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;
}
```
![图片说明](https://uploadfiles.nowcoder.com/images/20190809/308237136_1565350669146_1B24C97B8E440BEA2CAC473FDD658114 "图片标题")
由此可以看出
next_permutation(num,num+n)函数是对数组num中的前n个元素进行全排列,同时并改变num数组的值
![图片说明](https://uploadfiles.nowcoder.com/images/20190809/308237136_1565353829458_27223644DD20CA53E00456BA602FFCD1 "图片标题")
另::prex_permutation()应对的情况为{3,2,1}之类降序。
5.求字符串所有子串
```
void sub(string str) {
for (int i = 0; i < str.size(); i++)
for (int j = 1; j <= ((str.substr(i)).size()); j++)
cout << str.substr(i, j) << endl;
}
```
现在才仔细看原来是用了字符串类的方法。。。看样子这个类我了解的还是太少了
标签:return,int,ret,while,得到,奇技淫巧,iWeek,getchar 来源: https://www.cnblogs.com/xzmxiao/p/11372054.html