C语言C++

面向笔试C/C++编程:基础

2018-10-20  本文已影响200人  jdzhangxin

1. 基本框架

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

int main(){
    return 0;
}

2. 输入输出

No. 类型 变量 输出 输入
1 整型 int n = 0; printf("%d\n",n); scanf("%d",&n);
2 单精度浮点型 float f = 0; printf("%f\n",f); scanf("%f",&f);
3 字符串 string s; cout << s << "\n"; cin >>s;
4 字符 char c; printf("%c",c); scanf("%c",&c)
5 长整型 long long l; printf("%ldd",l); printf("%lld",&l);

变量名只能英文字母加下划线

小数精确度处理:%.精确度f

整数除法说明
整数除以整数还是整数,如果结果需要是小数,要乘以1.0。例如:
3/2结果为1,1.0*3/2结果为1.5

3. 字符串

  1. 字符串变量string s;
  2. 获取字符串长度s.size()
  3. 获取字符串i位置字符s[i]
  4. 遍历字符串
for(int i=0;i<s.size();++i){
    printf("%c",s[i]);
}
  1. i截取长度为len字符串s.substr(i,len);
  2. i截取到字符串结束s.substr(i);
  3. 字符串字典序排序sort(s.begin(),s.end());
  4. 在字符串后添加n个字符c s.append(n,c);
  5. 字符串连接+,str3 = str1+str2
  6. 构建一个n个字符c组成的字符串string(n,c)

4. 数组

4.1 一维数组

No. 操作 定长数组 变长数组
1 定义数组 int nums[n]; vector<int> nums;
2 访问下标i元素 nums[i] nums[i]
3 存放数据 nums[i] nums.push_back(数据)
4 数据数量 n nums.size()

4.2 二维数组

No. 操作 定长数组 变长数组
1 定义数组 int table[n][m]; vector<vector<int> > table;
2 行数 n table.size()
3 列数 m table[i].size()
4 获取第i行第j列数据 table[i][j] table[i][j]

5. 排序

sort()默认以字典序排列字符,以升序排列数字。

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

bool cmp(char a,char b) {
  return a > b;// 正确的顺序
}

int main() {
   string s; // s[i]
   cin >> s;
   sort(s.begin(),s.end(),cmp);
   cout << s;
   return 0;
}
#include <bits/stdc++.h>
using namespace std;

bool cmp(int a,int b){
  return a > b;// 正确的顺序
}

int main() {
   int n = 0;
   scanf("%d",&n);
   int nums[n];
   for(int i=0;i<n;++i){
     scanf("%d",&nums[i]);   
   }
   sort(nums,nums+n,cmp);
   for(int i=0;i<n;++i){
     printf("%d ",nums[i]);   
   }
   
   return 0;
}

6. 结构体

把多个变量绑到一起的一个类型,可以把结构体看成一个多个变量组成包。

struct 结构体{
  变量...
};

例如:
定义结构体

struct UserInfo{
  string name;
  int age;
  bool sex;
};

使用

UserInfo zhangsan;
zhangsan.name = "张三";
zhangsan.age = 31;
zhangsan.sex = true;

结构体数组

UserInfo users[n];
users[i].name
users[i].age
users[i].sex

7. 查找

8. 集合set

set<类型> 集合名
插入数据集合名.insert(数据);
删除数据集合名.erase(数据);
set里面没有重复的值,不能使用[下标]访问。
集合里面存放数据数目集合名.size()

set<string> resset;
resset.insert("00");
resset.insert("10");
resset.insert("11");
resset.insert("01");
resset.insert("00");
resset.insert("01");
resset.insert("00");
resset.insert("00");
resset.insert("01");
resset.insert("00");
resset.insert("01");
// 集合不能使用resset[0];
for(string s:resset){
    cout << s << '\n';
}

数据不允许重复,使用集合set<>

9. 基于范围for循环

int nums[4];
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
// 基于下标的for循环
for(int i=0;i<4;++i){
    printf("%d\n",nums[i]);
}
// 基于范围的for循环
for(int n:nums){
    // n相当于nums[i]
    printf("%d\n",n);
}

10. 指针

指针是用来存放变量的地址。使用类型 *定义指针,
*指针变量使用指针。
如果指针类型是结构体,使用->访问结构体的变量。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a = 0;
    printf("%d\n",a);
    int *p = &a;
    *p = 10;
    printf("%d\n",a);
}

11. 链表

数组类型 数组名[n]

#include <bits/stdc++.h>
using namespace std;
// 链表节点 = 数据 + 索引(指针) 
struct Node{
  int data;
  Node *p;
};
// 10 7 5 3 2 1
int main() {
    // 构造链表
    Node head; // 头节点(链表的首节点)
    head.data = 10;
    
    Node n1;
    n1.data = 7;
    head.p = &n1;
    
    Node n2;
    n2.data = 5;
    n1.p = &n2;
    
    Node n3;
    n3.data = 3;
    n2.p = &n3;
    
    Node n4;
    n4.data = 2;
    n3.p = &n4;
    
    Node n5;
    n5.data = 1;
    n4.p = &n5;
    n5.p = NULL; // n5后面没有节点了NULL。
    
    // 遍历链表
    Node* pNode = &root;
    while(pNode != NULL){
        printf("%d ",pNode->data);
        pNode = pNode->p;
    }
}

先进先出LIFO
使用vector<>模拟栈。

    vector<char> test;
    test.push_back('/');
    test.push_back('a');
    test.push_back('b');
    test.push_back('c');
    test.pop_back();
    test.push_back('d');
    test.pop_back();
    test.pop_back();
    for(int i=0;i<test.size();++i){
        cout << test[i] << "\n";
    }

栈的主要用途是,解决括弧匹配问题。

#include <bits/stdc++.h>
using namespace std;
bool check(string s){
    vector<char> stack;
    for(int i=0;i<s.size();++i){
        if(s[i] == '['){
            stack.push_back('[');
        }else if(s[i] == ']'){
            if(stack.empty()){// 如果前面没有做左括号,不匹配
                return false;
            }
            stack.pop_back();
        }
    }
    return stack.empty();
}
int main(){
    string s = "[[[]]][][][][][][][]";
    
    printf("%d\n",check(s));
    
    return 0;
}

特殊函数

累加函数accumulate(first,last,init),累加序列[first,last)区间中的数据,init是累加初始值

struct Node{// 双亲孩子表示法
    vector<int> children;
};

void DFS(int root,Node* nodes){// 深度优先遍历
    stack<int> s;
    s.push(root);
    while(!s.empty()){
        int now = s.top();
        s.pop();
        printf("%d ",now); // 出栈访问
        vector<int> children = nodes[now].children;
        for(int i=children.size()-1;i>=0;--i){
            int post = children[i];
            s.push(post);
        }
    }
}

void BFS(int root,Node* nodes){// 广度优先遍历
    queue<int> s;
    s.push(root);
    printf("%d ",root); // 入队访问
    while(!s.empty()){
        int now = s.front();
        s.pop();
        vector<int> children = nodes[now].children;
        for(int i= 0;i<children.size();++i){
            int post = children[i];
            s.push(post);
            printf("%d ",post); // 入队访问
        }
    }
}

深度遍历递归写法

void DFS2(int root,Node* nodes){// 深度优先遍历
    printf("%d ",root); // 出栈访问
    vector<int> children = nodes[root].children;
    for(int i= 0;i<children.size();++i){
        int post = children[i];
        DFS2(post,nodes);
    }
}
上一篇下一篇

猜你喜欢

热点阅读