算法从零开始 c/c++基础 查漏补缺

算法从零开始 一

算法学的零零散散,蓝桥勉强拿个国三,ACM惨败,称着假期,好好从头开始,也是学长建议,刷算法笔记

前面是基础、浏览一遍,查漏补缺即可:

1.绝对值在10^9范围以内的整数都可以定义成int型

2.题目要求超过2147483647,如10^10或者10^18次方,就得用long long来存储

3.双精度double,8B,64位,有效精度15~16位

4.0~9的ASCII:48~57        A~Z的ASCII:65~90        a~z的ASCII:97~122

5.cin、cout输出所耗费的时间远远多于scanf和printf

6.%md m位右对齐输出,高位补空格

7.%0md  m位右对齐输出,高位补0

8.floor(double x):向下取整  ceil(double x) :向上取整

9.power(double r,double p) :r^p         pow(double x,int n):x,n

10.log(double x)   lnx  x以自然对数为底的对数、C语言中没有对任意底数求对数的函数,必须用换底公式来求 eg:logab=logeb/logea

11.round(double x) 对x四舍五入

12.memset函数 memset(数组名,值,sizeof(数组名)) (需要#include<string.h>)  memset按字节赋值,每个字节赋一样的值,由于0的二进制补码全为0,-1的二进制补码全为1,初学赋值0,-1不易弄错,赋其他值要用fill函数

eg:memset(a,0,sizeof(a))全部赋值0

      memset(a,-1,sizeof(a))全部赋值-1

13.fill函数   fill(a,a+5,233)数组a[0]~a[4]均赋值为233  比memset好用,不过效率低一些,且包含在algorithm里

14.sscanf与sprinf   (stdio.h下)

1.sscanf理解为:string+scanf 从string里面读

char str[100];

sscanf(str,"%d",&n); 从左到右将str中的内容以"%d"形式传到n里     string->int     

       string可以非法,遇到非法字符自动停止       eg: sscanf("2.2a2","%lf",&doubl);  doubl被赋值 2.2

2.sprintf理解为:string+printf   输出到string里

char str[100];

sprintf(str,"%d",n);   从右到左将n以"%d"的格式写到str字符数组里  int->string

3. string转int还可以用   int atoi(char*);   eg:int n=atoi("12345")+1;    int n=atoi(str.c_str());

15.一般的OJ系统,一秒能承受的运算次数大概是10^7~10^8

16. 文件结束符结束

        while(scanf("%d",&a)!=EOF){}

        while(scanf("%d",&a)!=-1){}

        while(cin>>a){}

        while(scanf("%s",str)!=EOF){}

        while(gets(str)!=NULL){}

17 自定义数据类型 typedef long long ll;

18%lld输出long long

19 c++switch后的case写“str"再用switch(string)无法匹配所以switch后面的常量表达式并不是可以支持任意类型,char 和 int 用switch 比较合适,其他类型不建议使用switch

20.freopen("input.txt","r",stdin);//从文件input中读数据 不用自己一个一个输入   文件可以写绝对路径,相对路径默认根目录是.exe目录
    freopen("out.txt","w",stdout);//输出到文件out.txt内 不输出到控制台

21. INT_MAX int最大值                                     INT_MIN int最小值

 

 

 

1、memset()函数只能初始化0和-1

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

void print(int a[10]){
	for(int i=0;i<10;i++){
		cout<<a[i]<<" ";
	}
	cout<<endl;
}

int main(){
	int m[10];
	memset(m,0,sizeof(m));
	print(m);
	memset(m,1,sizeof(m));
	print(m);
	memset(m,-1,sizeof(m));
	print(m);
	
	return 0;
}

 

2、string转int还可以用   int atoi(char*);   eg:int n=atoi("12345")+1;    int n=atoi(str.c_str());

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	string str;
	
	//atoi()   char*转int 
	str="12345";
	n=atoi(str.c_str());
	cout<<n+1;
	//还有一个c++函数  stoi(str)直接sting转int 可惜太新了 devc用不了 
	
	cout<<"\n遇到字母自动停止:\n"; 
	cout<<atoi("12345abc")+1<<endl;
	cout<<atoi("12a345abc")+1<<endl;
	
	return 0;
}

 

3、sscanf 和atoi一样 遇到非法字符会自动停止 开始就非法 则相当于没赋值

#include<bits/stdc++.h>
using namespace std; 
int main(){
	double d;
	cout<<"sscanf 和atoi一样 遇到非法字符会自动停止 开始就非法 则相当于没赋值:\n"; 
	sscanf("2.22","%lf",&d);cout<<d+1<<endl;
	sscanf("2.22aa","%lf",&d);cout<<d+1<<endl;d=-1;
	sscanf("aa2.22","%lf",&d);cout<<d+1<<endl;//相当于没赋值 
	sscanf("2a.22","%lf",&d);cout<<d+1<<endl;
	sscanf("2.2a2","%lf",&d);cout<<d+1<<endl;
	
	cout<<"\nsprinf 转string,被转第三个参数非法直接编译不过:\n";
	char str[100];
	sprintf(str,"%lf",2.789);cout<<str<<endl; //不存在非法了 
	sprintf(str,"%d",atoi("278a9"));cout<<str<<endl; //属于自找麻烦 
	
	return 0;
} 
// sscanf 和atoi一样 遇到非法字符会自动停止 开始就非法 则相当于没赋值

 

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值