OI常用函数

举报
秋名山码民 发表于 2022/05/29 14:16:39 2022/05/29
【摘要】 cmath@函数名称: abs函数原型: int abs(int x);函数功能: 求整数x的绝对值函数返回: 计算结果参数说明:所属文件: <math.h>,<stdlib.h>//包含其中任意一个即可使用使用范例:#include <stdio.h>#include <math.h>int main(){ int number=-1234; ...
  1. cmath
    @函数名称: abs
    函数原型: int abs(int x);
    函数功能: 求整数x的绝对值
    函数返回: 计算结果
    参数说明:
    所属文件: <math.h>,<stdlib.h>
    //包含其中任意一个即可使用
    使用范例:
#include <stdio.h>
#include <math.h>
int main()
{
   int number=-1234;
   printf("number: %d absolute value: %d",number,abs(number));
   return 0;
}

@函数名称: fabs
函数原型: double fabs(double x);
函数功能: 求x的绝对值.
函数返回: 计算结果
参数说明:
所属文件: <math.h>
使用范例:

#include <stdio.h>
#include <math.h>
int main()
{
    float  number=-1234.0;
    printf("number: %f absolute value: %f",number,fabs(number));
    return 0;
}

还有个求复数绝对值,在OI中一般不用,可做了解:
@函数名称: cabs
函数原型: double cabs(struct complex znum)
函数功能: 求复数的绝对值
函数返回: 复数的绝对值
参数说明: zuum为用结构struct complex表示的复数,定义如下:
struct complex{
double m;
double n;
}
所属文件: <math.h>

#include <stdio.h>
#include <math.h>
#include<complex.h>//一定要加
struct complex
{
      double m;
      double n;
 };
int main()
{
    struct complex z;
    double val;
    z.x=2.0;
    z.y=1.0;
    val=cabs(z.y-z.x);
    printf("The absolute value of %.2lfi %.2lfj is %.2lf",z.x,z.y,val);
    return 0;
}

@函数名称: ceil

函数原型: double ceil(double num)

函数功能: 得到不小于num的最小整数

函数返回: 用双精度表示的最小整数

参数说明: num-实数

所属文件: <math.h>

#include <math.h>

#include <stdio.h>

int main()

{

    double number=123.54;

    double down,up;

    down=floor(number);

    up=ceil(number);

    printf("original number     %5.2lf",number);

    printf("number rounded down %5.2lf",down);

    printf("number rounded up   %5.2lf",up);

    return 0;

}

@函数名称: floor

函数原型: double floor(double x);

函数功能: 求出不大于x的最大整数.

函数返回: 该整数的双精度实数

参数说明:

所属文件: <math.h>
代码类比与ceil

@函数名称: sqrt

函数原型: double sqrt(double x);

函数功能: 计算x的开平方.

函数返回: 计算结果

参数说明: x>=0

所属文件: <math.h>

#include <math.h>

#include <stdio.h>

int main()

{

    double x=4.0,result;

    result=sqrt(x);

    printf("The square root of %lf is %lf",x,result);

    return 0;

}

最后还有已经几年不考的三角函数我就简单介绍一下
@函数名称: sin

函数原型: double sin(double x);

函数功能: 计算sinx的值.正弦函数

函数返回: 计算结果

参数说明: 单位为弧度

所属文件: <math.h>

使用范例:

#include <stdio.h>

#include <math.h>

int main()

{

    double result,x=0.5;

    result=sin(x);//cos,tan类比

    printf("The sin() of %lf is %lf",x,result);

    return 0;

}
  1. stdio.h

头文件#include<stdio.h>中
stdio.h是stand input & output的缩写,意思是标准输入输出头文件。凡是用到标准输入输出函数,就要调用该头文件。
查看stdio.h目录下包含哪些函数:主要有文件访问、二进制输入/输出、格式化和非格式化输入/输出、文件定位、错误处理、文件操作等。

常用标准输入输出函数:
scanf() 从屏幕格式输入
printf() 格式输出到屏幕
getchar() 从屏幕得到一个字符
putchar() 字符输出到屏幕
gets() 从屏幕得到一个字符串
puts() 字符串输出到屏幕
{
fscanf() 从磁盘格式输入
fprintf() 格式输出到磁盘
fgetc() 从磁盘得到一个字符
fputc() 字符输出到磁盘
fgets() 从磁盘得到一个字符串
fputs() 字符串输出到磁盘
}//f是OI中几乎不用的
#号是预处理语句,表明在编译之前预先进行处理。
.h是header file的缩写,表面这是一个头文件。
include是文件包含命令,后面跟着引号"“或者尖括号<>,意思是将引号或尖括号内指定的文件包含到本程序中,成为本程序的一部分,而包含的文件通常是由系统提供的。
区分引号”“或者尖括号<>:尖括号<>编译程序会先到标准函数库中找文件,引号”"编译程序会先从当前目录中找文件。

还有OI必须要用的文件操作

#include <cstdio>
#include <iostream>
using namespace std;
int main() 
{ 
	int a,b; 
	freopen("debug\\in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取 
	freopen("debug\\out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中 
	while(cin>>a>>b) 
	cout<<a+b<<endl; // 注意使用endl 
	fclose(stdin);//关闭文件 
	fclose(stdout);//关闭文件 
	return 0; 
}

stdlib.h
随机数

#include<iostream>
#include<iomanip>//包含setw()函数
#include<ctime>
#include<cstdlib>//包含srand()函数和rand()函数
using namespace std;

int main()
{
    srand((unsigned)time(NULL));//用系统时间产生随机数种子
    for(int i=0;i<4;i++){
        for(int j=0;j<5;j++){
            //利用不同的随机数种子产生不同的随机数序列
            //rand()%x  表示输出的随机数在[0,x)之间
            //setw(2)表示输出内容占2个空格
            cout<<setw(2)<<rand()%10;
        }
        cout<<endl;
    }
    return 0;
}

rand()函数的使用通式:
取得[0,x)的随机整数:rand()%x;
取得(a,b)的随机整数:rand()%(b-a-1)+a+1;
取得[a,b)的随机整数:rand()%(b-a)+a;
取得[a,b]的随机整数:rand()%(b-a+1)+a;
取得(a,b]的随机整数:rand()%(b-a)+a+1;
取得0-1之间的浮点数:rand()/double(RAND_MAX)

感觉有点对不起大家,这几个月都没有更新,忙着补文化课,这周给大家补充完整。
附加几个骗分技巧

3.string字符串
1.构造函数
语法不适合OI,直接上例子:

string str1="abcd";
string str2("abcd");
string str3=(str2,1,3);//str3="bcd"
  1. 操作符
==    
  >
  <
  >=
  <=
  !=
  +
  +=
  []

例子:

string str1="abcd";
string str2="defg";
string str3=str1;
string str4=str1+str2;

if(str3==str1)  cout<<"true";  //true
if(str2>str1)   cout<<"true";  //true

//输出str4
//法一
cout<<str4;
//法二
for(int i=0;i<str4.length();i++)
{
    cout<<str4[i];
}

可以用==,>,<,>=,<=,!=比较两个字符串,用+,+=连接两个字符串,用[]来取特定的字符。
3. length()函数

string str="abcd";
cout<<str.length(); //4

length()函数返回字符串的长度. 这个数字应该和size()返回的数字相同.
size()与length()无任何区别

string str="abcd";
cout<<str.size(); //4
  1. begin()函数:指向第一个元素
  2. end()函数:指向末元素(最后一个字符的下一个位置’\0’)
  3. compare()函数
string str1="this is a nice day!";
string str2="nice";
string str3="it is nice!";
char s[]="nice";
cout<<str1.compare(str2)<<endl;   //1
cout<<str2.compare(s)<<endl;       //0
cout<<str1.compare(10,4,str2)<<endl;  //0
cout<<str1.compare(10,4,str3,6,4);    //0

当str1=str2时返回‘0’
8. swap()函数
swap()函数把str1和str2字符串交换。

string str1="good";
string str2="great";
str1.swap(str2);//str1="great";str2="good";

9、copy()函数 copy复制

string str1="this is a nice day!";
char str2[20];
str1.copy(str2,4,10);
//输出
for(int i=0;i<4;i++)
{
    cout<<str2[i];
}

copy()函数拷贝自己的num个字符到str中。返回值是拷贝的字符数。

大概就这么多吧,以后想起再补充。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200