我整理的可以调用的常用函数各类函数

一、sort函数
降序,从大到小
bool cmp(int a,int b){
return a>b;
}
sort(s+1,s+n+1,cmp);
升序从小到大
bool cmp(int a,int b){
return a<b;
}
sort(s+1,s+n+1);
https://zhidao.baidu.com/question/323558933.html
sort的头文件是algorithm

//这是函数是sort函数的第三个参数
//如果希望升序排序,就是"<",降序排列就是">"号
//如果希望用其他的参数作为排序条件,只需要把相应的条件改一下(如果改成name),这样结构体就以name作为排序标准
bool comparison(student a,student b){
return a.achievement<b.achievement;
}

1.默认
的sort函数是按升序排。对应于1)
sort(a,a+n); //两个参数分别为待排序数组的首地址和尾地址
2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)
例如:
int cmp( const int &a, const int &b ){
if( a > b )
return 1;
else
return 0;
}
sort(a,a+n,cmp);
是对数组a降序排序
又如:
int cmp( const POINT &a, const POINT &b ){
if( a.x < b.x )
return 1;
else
if( a.x == b.x ){
if( a.y < b.y )
return 1;
else
return 0;
}
else
return 0;
}
sort(a,a+n,cmp);

==================================================================================================
  二、一个封装好的求最大值的函数

int  max(int a[],int n)
{
	int i,max=0;
	for(i=1;i<=n;i++)
		if(a[i]>max)
			max=a[i];
	return max;
}

======================================================================================
3. sqrt()开平方
注意 头文件为cmath

#include<iostream>
#include<cmath>
using namespace std;

int main(){
	int n;
	scanf("%d",&n);
	cout<<sqrt(n)<<endl;
	return 0;
}
/*
17
4.12311
Press any key to continue
*/

这里sqrt函数出来的是浮点型
所以如果要整形,用(int)sqrt(17)结果为4

4.求最大公因数
(1)辗转相除法

int gcd(int a,int b){
	int c;
	while(b){
	c=a%b;
	a=b;
	b=c;
	}
	return a;
}
int gcd(int a,int b){
	return b==0?a:gcd(b,a%b);
}

(2).辗转相减法

int gcd(int a,int b){
	return a==b?a:gcd(a>b?a-b:a,b>a?b-a:b);
}

==============================================================================================
5.C库函数-tolower()
描述 c库函数 int tolower(int c)把给定的字母转化为小写字母
声明 下面是tolower()函数的声明 int tolower(int c)
参数 c-这是要被转换为小写的字母
返回值 如果c有相应的小写字母,则该函数返回c的小写字母, 否则c保持不变。返回值是一个可被隐式转化为char类型的int值。
实例 下面的实例演示了tolower()函数的用法

#include<stdio.h>
#include<ctype.h>

int main(){
	int i=0;
	char c;
	char str[]="RUNOOB";
	
	while(str[i]){
		putchar(tolower(str[i]));
		i++;
	}

	return 0;
}
运行结果
runoob
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值