保研考研机试攻略:第一章——从零开始

🍨🍨🍨欢迎大家来到保研考研机试攻略专栏,该专栏将更新我对N诺平台的计算机考研机试攻略——高分篇、满分篇教程的学习笔记和心得,N 诺是唯一 一个纯粹为计算机考研而准备的学习平台,学完这些教程的内容,相信我们都会拿到满意的机试高分,如果你也对机试考试的准备感到迷茫,来和我一起学习吧~有任何问题欢迎评论区留言或私信我,让我们一起拿捏机试,顺利上岸!!!

🧊🧊🧊1.1 输入输出技巧

🥥(1)基本类型输入输出

  • 输入int型变量 scanf("%d",&i);
  • 输入long long型变量 scanf("%lld",&ll)
  • 输入double型变量 scanf("%lf",&d);
  • 输入char型变量 scanf("%c",&c);
  • 输入string型变量 scanf("%s",s);
  • 输出 printf("%d %lf %c %lld %s\n",&i,&d,&c,&ll,s);

🥥(2)gets、getchar和putchar的使用

char s[200];
gets(s);//可输入带空格的字符,如Hello world!
printf("%s\n",s);

char c=getchar();//只能输入一个字符,可用作string间的空格的消除
putchar(c);

🥥(3)输出进制转换

int a=10;
printf("%x\n",a);//小写十六进制输出,a
printf("%X\n",a);//大写十六进制输出,A
printf("%o\n",a);//八进制输出,12

itoa(num,string,2);//转二进制
itoa(num,string,10);//转十进制
//这个函数用的时候注意有些oj环境即使用万能头也不支持

🥥(4)输出增加前置0

int a=1;
printf("%02d\n",a);//2表示宽度,不够补0:输出01

🥥(5)输出保留小数

//c语言输出固定位数小数
double a=5.2;
printf("%.2lf\n",a);//输出保留两位小数:输出5.20

//c++输出固定位数小数
cout<<fixed<<setprecision(n)<<a;//保留n位小数

//四舍五入保留两位小数
double rounded=std::round(a*100)/100;

🥥(6)输出字符ASCII码

printf("%d\n",'a');//97
printf("%d\n",'A');//65

提示:printf和cout尽量不要同时使用,会发生一些不可控的意外。但是建议在代码中c与c++语法混用,用c++提交。一般c++输入输出更方便些,建议使用。当输入输出有特殊要求时,cin和cout不好解决时,还是用scanf和printf解决方便一些。

🥥(7)cin、cout加速

ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);

🥥(8)其他经验

计算整数n的位数

int n;
int bit=static_cast<int>(log10(n))+1;

不同输入结束的处理

//1告诉有n组,直接for循环输入
for(int i=0;i<n;i++){}

//2以“0 0”表示输入结束
cin>>a>>b;
while(a!=0&&b!=0){//这里处理完数据记得cin>>a>>b;}

//3什么信息也不给,自己判断后方是否还有输入,但是知道一行要输入几个数
while(cin>>a>>b){}或while(scanf("%x %x",&a,&b)!=EOF){}

//4连输入一行几个数也不知道
while(getline(cin,s)){}

字符串转整数

string s;
cin>>s;
for(int i=0;i<=length;i++)
{
    stringstream ss;
    ss<<s[i];
    ss>>n;
    num=num*10+n;
    n=0;
}

整数转字符串

int x;
string s=to_string(x);

🧊🧊🧊1.2 头文件问题

推荐使用万能头文件:#include<bits/stdc++.h>,一般系统都支持使用。

🧊🧊🧊1.3 时间复杂度是否可行

给出一个对应关系,以时限为1s为例:

时间复杂度N的可行大小
O(N)500W(10^7)左右
O(NlogN)20W(10^6)左右
O(N^2)2000左右
O(N^2logN)700左右
O(N^3)200左右
O(N^4)50左右
O(2^N)24左右
O(N!)10左右

特殊技巧:发现算法可能超时(一般是暴力),先把这道题留一下,做其他题,可以去看一下排行榜,如果过这道题的人多,说明这道题可能数据比较水,直接暴力,出题人可能偷懒或者失误了导致数据很水。考研机试的题目数据大部分情况都比较水,不要被复杂度吓唬住了,后面会教大家如何用优雅的技巧水过去不会优化的算法。

🧊🧊🧊1.4 STL

STL的函数多在algorithm头文件中,可以直接拿来用。

🥥(1)排序sort

时间复杂度O(NlogN),对容器如struct用sort需要重载小于号

sort(begin,end+1,cmp);
//三个参数(最后一个参数可以没有)
//第一个是排序区间起点
//第二个是排序区间终点+1
//第三个是排序方法函数,默认升序,降序:greater<int>()

//n个数的数组
sort(a,a+n);

🥥(2)查找lower_bound&upper_bound

时间复杂度O(logN)

  • lower_bound(begin,end+1,num):大于等于num的第一个位置
  • upper_bound(begin,end+1,num):大于num的第一个位置

两个函数都是采用二分查找的方法在一个排好序的数组中进行的查找

//n个数的数组
int pos1=lower_bound(a,a+n,11)-a;
int pos2=upper_bound(a,a+n,11)-a;

两个函数返回的都是地址,不存在就返回end+1,通过返回地址减去起始地址begin得到找到数字在数组中的下标。

  • lower_bound(begin,end+1,num,greater<int>()):小于等于num的第一个位置
  • upper_bound(begin,end+1,num,greater<int>()):小于num的第一个位置

🥥(3)优先队列priority_queue(堆)

  • q.push(num)
  • q.empty()
  • q.top()
  • q.pop()
#include <iostream>
#include <queue>
using namespace std;
int main() {
    priority_queue<int> q;//定义一个优先队列,默认大根堆,小根堆在int后加上greater<int>
    q.push(1);//入队
    q.push(2);
    q.push(3);
    while (!q.empty()) {//判读队列不为空
        cout << q.top() << endl;//队首元素
        q.pop();//出队
    }
    return 0;
}

🥥(4)vector

  • a.back()=4;
  • a.clear()
  • a.push_back(num)
  • a.pop_back(num)
  • a.resize(11):多删少补,默认补0
  • a.resize(11,5):补5
  • a.size()
  • a.empty()
#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> v;//定义一个空的 vector
    for (int i = 1; i <= 10; ++i) {
        v.push_back(i * i); //加入到 vector 中
    }
    for (int i = 0; i < v.size(); ++i) {
        cout << v[i] << " "; //访问 vecotr 的元素
    }
    cout << endl;
    return 0;
}

🥥(5)queue

#include <iostream>
#include <queue>
using namespace std;
int main() {
    queue<int> q;//定义一个队列
    q.push(1);//入队
    q.push(2);
    q.push(3);
    while (!q.empty()) {//当队列不为空
        cout << q.front() << endl;//取出队首元素
        q.pop();//出队
    }
    return 0;
}

🥥(6)stack

  • s.push(num)
  • s.top()
  • s.pop()
  • s.empty()
  • s.size()
#include <iostream>
#include <stack>
using namespace std;
stack<int> S;//定义一个栈
int main() {
    S.push(1);//入栈
    S.push(10);
    S.push(7);
    while (!S.empty()) {//当栈不为空
        cout << S.top() << endl;//输出栈顶元素
        S.pop();//出栈
    }
    return 0;
}

🥥(7)map

<key,value>键值对结构

  • map<int,bool> mp;
  • mp[13]=true;
  • mp.find(13)!=mp.end();
  • mp.count(12);
  • mp.erase(12);
  • mp.clear();
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
    map<string, int> dict;//定义一个 map
    dict["Tom"] = 1;//定义映射关系
    dict["Jone"] = 2;
    dict["Mary"] = 1;
    if (dict.count("Mary")) {//查找 map
        cout << "Mary is in class " << dict["Mary"];
    }
    //使用迭代器遍历 map 的 key 和 value
    for (map<string, int>::iterator it = dict.begin(); it != dict.end(); ++it) {
        cout << it->first << " is in class " << it->second << endl;
    }
    dict.clear();//清空 map
    return 0;
}

🥥(8)set

默认升序排列,且去重

  • s.insert(3);
  • s.erase(3);
  • auto it1=s.find(4),it2=s.find(6);
  • if(it1==s.end()) cout<<"no";
  • s.count(1);
#include <iostream>
#include <set>
using namespace std;
int main() {
    set<string> country;//定义一个存放 string 的集合
    country.insert("China");//插入操作
    country.insert("America");
    country.insert("France");
    set<string>::iterator it;
    //使用迭代器遍历集合元素
    for (it = country.begin(); it != country.end(); ++it) {
        cout << * it << " ";
    }
    cout << endl;
    country.erase("American");//删除集合内的元素
    country.erase("England");
    if (country.count("China")) {//统计元素个数
        cout << "China in country." << endl;
    }
    country.clear();//清空集合
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值