机试学习笔记01 --梦的开始

1. 输入输出

输入带空格

scanf()不行,用gets()
scanf(”%d“)不会读取回车,但gets()会遇到回车结束,所以中间要用getchar()吃掉回车
scanf("%c")会读取回车为一个字符,scanf("%s")不会读取回车,会丢掉,故二维地图输入不能用%c

输出进制转换

%x 小写16进制输出
%X 大写16进制输出
%o 八进制输出

输出增加前置0

%02d 2表示宽度,0表示0补充

输出保留小数

%.2lf 2表示保留两位小数

long long

%lld 1e-18 ~ 1e18

cin和cout

cin>>
cout<<
<<endl 表示结束
注意cout不要和printf同时用

2. 头文件技巧

万能头文件

#include<bits/stdc++.h>
using namespace std;

完整头文件

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include
#include
#include
#include
#include
#include
using namespace std;

3. 数组使用技巧

可以用数组来标记!即将输入作为索引,存储出现次数。还可以嵌套使用数组来标记,例如将次数作为索引,存储输入值。

#include <bits/stdc++.h>
using namespace std;

int in[105] = {0};
int num[105] = {0};
int main()
{
	int n, x;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &x);
		in[x]++;
	}
	for(int i = 0;i <= 100; i++){
		num[in[i]] = i; 
	}//num中索引是次数,值是数字 
	for(int i = 1; i <= 100; i++){
		if(num[i] > 0){//值为0的无效,因为输入数字大于0 
			printf("%d %d\n", num[i], i);
		}
		
	}
	
	return 0;
}

4. 审视时度–复杂度是否可做

评测机大概1s可以跑1e7条语句
O(N) – N最大500w
O(NlogN) - - N最大20w
O(N^2) – N最大2000
O(2^N) – N最大24
O(N!) – N最大10

5. C++ STL使用

排序:

sort() 参数:排序起点,排序终点+1,比较函数(默认从小到大),从大到小则自己写cmp

int cmp(int a, int b)
{
	return a>b;
}

查找:

lower_bound(begin, end, num) :从begin开始到end-1的第一个大于等于 num的数,找到则返回地址,用地址减去begin,得到数字在数组中的下标;未找到则返回end
upper_bound(begin, end, num):从begin开始到end-1的第一个大于 num的数,找到则返回地址,用地址减去begin,得到数字在数组中的下标;未找到则返回end
都是二分查找,在排序好的数组中查找,此处讲解为从小到大排序。若为从大到小,则找小于等于和小于,参数修改为
lower_bound(begin, end, num,greater())

优先队列:

priority_queue q;
可以让队列中元素降序排列。

vector:

vector v;
不用确定长度,可以变长的数组。用v.push_back()加入。
注意,如果要用二维的,type = int* 即可。

queue:

queue q;
可用数组代替,但方便使用不同的type的队列。

stack:

stack s;
同上。

map:

map(type,type) dict;
可以定义一个字典映射

#include <bits/stdc++.h>
using namespace std;

int main()
{
	map<string, int> dict;
	dict["TOM"] = 1;
	dict["JONE"] = 2;
	dict["MARY"] = 1;
	//map<string, int>::iterator it 生成一个map容器的正向迭代器,类似指针 
	for(map<string, int>::iterator it = dict.begin(); it != dict.end(); ++it){
		cout << it->first << " " << it->second <<endl;
	}
	dict.clear();
	return 0;
}

查找类,用map

set:

set country;
集合,去重,其它和map类似。
插入用insert(),删除用erase()

Tips:类,作用域标识符

作用域标识符 ‘::’
子类标识符 ‘:’
类访问标识符:
默认为private
public 可以在类外部访问。继承时非私有的访问方式不变。
protect 在类外部无法访问,但在子类中可以访问。继承时非私有的访问方式变为protect。
private 只能在类中访问。继承时非私有的访问方式变为private。
默认private继承

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值