本文用来存储我用到过的数学方法函数,随着学习深入不断增加
声明
//类型转换
int string2int(string s);//string-->int
string int2string(int n);//int-->string
//数组随机打乱方法
void disorder(int a[], int n);//disorganize an array,
//every element has the same possibility to be set in different place
//随机数生成方法
int random(int start, int end);//生成闭区间[start,end]的整随机数
实现
int string2int(string s){
int num;
stringstream stream;
stream << s;
stream >> num;
return num;
}
string int2string(int n){
stringstream ss;
ss << n ;
string str = ss.str();
return str;
}
void disorder(int a[], int n){
int index, tmp, i;
srand(time(NULL));
for(i=0; i<n; i++)
{
index=rand()%(n-i)+i;
if(index!=i)
{
tmp=a[i];
a[i]=a[index];
a[index]=tmp;
}
}
}
int random(int start, int end){
return (rand()%(end - start + 1)) + start;
}