chatgpt教我准备计算机考研复试C++机试(学习记录)(4)

1.使用str存储数字时,在运算时要把str转成数字,否则会出错

skew = skew + (str[i] - '0') * (pow(2, str.length()-i)-1);

2.强制类型转换

//int转string
int num = 123;
string str = to_string(num);

//string转int
string str = "123";
int num = stoi(str);

要注意的是转为string类型后,如果需要比较,要用if(str[i] == '3'),注意类型已经转为char了

3. 并查集

int root[100];		//用一个数组表示并查集

//初始化并查集
void Init(int root[]){
    for(int i=0;i<100;i++)			
        root[i]=-1;
}

//Find “查”操作,找x所属集合(返回x所属根结点)
int Find(int a[],int x){
    while(a[x]>=0)			//循环寻找x的根
        x=a[x];
    return x;		
}

//Union 
void Union(int S[],int Root1,int Root2){
  	if(Root1==Root2)	return;		
  	//将根Root2连接到另一根Root1下面
    S[Root2]=Root1;
}
//Union “并”操作,小树合并到大树
void Union(int S[],int Root1,int Root2){
	x = Find(x);
	y = Find(y);
	if (root1 == root2){
		return;
	}

  	if(S[Root2]>S[Root1]) {	//Root2结点数更少
      	S[Root1] += S[Root2];	//累加结点总数
      	S[Root2]=Root1; //小树合并到大树
    } else {
		S[Root2] += S[Root1];	//累加结点总数
      	S[Root1]=Root2; //小树合并到大树    		 
    }
}
//Find “查”操作优化,先找到根节点,再进行“压缩路径”
int Find(int S[],int x){
  	int root = x;
    while(S[root]>=0)  
           root=S[root]; //循环找到根
    while(x!=root){	//压缩路径
      int t=S[x];		//t指向x的父节点
      S[x]=root;		//x直接挂到根节点下
      x=t;
    }
    return root;						//返回根节点编号
}

4.十进制和二进制的转换

#include <bitset>

// 十进制转二进制
string ToB(int n) {
    string s;
    while (n> 0) {
        int t= n % 2;
        s= to_string(t) + s;
        n /= 2;
    }
    return s;
}

// 二进制转十进制
int ToD(string& s) {
    bitset<32> binary(s);
    int n= binary.to_ulong();
    return n;
}

5.需要最大、最小整数值时

#include <climits>

int max = INT_MIN;
int min = INT_MAX;

6.getline用法

while (cin >> n){
    cin.ignore(); // 清除换行符
    getline(cin, str);//空格也输入
//要注意的是有可能会把前面的换行符也算上,从而出错
    ... ...    
}

7.字符串逆序

char str[] = "Hello, World!";
//对str 进行逆序
    strrev(str);

//或者
#include <algorithm>

reverse(str.begin(), str.end());

8.数组按列排序

    for(int i=0 ; i<6 ; i++){
        for(int j=0 ; j<4 ; j++){
            for (int k=j+1 ; k<5 ; k++){
                if(a[j][i] > a[k][i]){
                    swap(a[j][i], a[k][i]);
                }
            }
        }
    }

9.一个例题,注意str.erase的用法

#include <iostream>
using namespace std;

int main(){
	string str;
	char c;
	cin >> str >> c;
	for (int i=0 ; i<str.length() ; i++){
		if (str[i] == c){
			str.erase(i,1);
			i--;    //更新索引,不更新会出错
		}
	}
	for (int i=0 ; i<str.length() ; i++){
		cout << str[i];
	}
	
	return 0;
}

10.链表

//遍历链表统计和给定的已知变量相等的节点的个数
#include <iostream>
using namespace std;

struct Lnode{
	int data;
	Lnode *next;
};

int main(){
	int n, x;
	Lnode *l = new Lnode();	// 初始化链表头节点
	Lnode *q = l ;
	int count = 0;
	
	cin >> n >> x;
	for (int i=0 ; i<n ; i++){
		Lnode *p = new Lnode();
		cin >> p->data ;
		q->next = p;
		q = q->next;
	}
	
	Lnode *s = l->next;
	while(s != nullptr){
		if(s->data == x){
			count ++;
		}
		s = s->next;
	}
	
    Lnode *t = l->next;
    while (t != nullptr) {
        Lnode *m = t;
        t = t->next;
        delete m;
    }	
	
	cout << count;
	
	return 0;
}
链表的详细用法:
struct Node {
    int data;
    Node* next;
};
Node* head = nullptr; // 初始化链表头指针为空

//头插法
Node* newNode = new Node;
newNode->data = value; // 设置节点数据
newNode->next = head; // 将新节点的next指针指向当前头节点
head = newNode; // 更新头指针指向新节点
//尾插法
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr; // 新节点的next指针指向null

    Node* temp = head;
    while (temp->next != nullptr) 
        temp = temp->next;
    temp->next = newNode; // 找到链表末尾,将新节点连接到末
//遍历
Node* temp = head;
while (temp != nullptr) {
    // 处理节点数据
    cout << temp->data << " ";
    temp = temp->next; // 移动到下一个节点
}
//删除
if (head != nullptr) {
    Node* temp = head;
    head = head->next; // 更新头指针
    delete temp; // 释放内存
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
ECNU计科考研复试是上海东华大学计算机科学与技术专业硕士研究生复试环节的一部分。一般包括计算机基础知识测、编程实践、算法设计与分析、数据结构、数据库等内容。 的目的是通过实际操作和任务完成,评估考生的计算机基础知识、编程能力和解决问题的能力。一般会提供一些实际问题,考生需要根据题目要求进行编程实现,并实现功能要求以及考察的相关知识点。的题目会有一定的难度,需要考生具备扎实的计算机基础知识和编程实践经验。 针对ECNU计科考研复试准备,考生可以从以下几个方面进行: 1. 夯实计算机基础知识:系统复习计算机组成原理、操作系统、数据结构、数据库等相关课程的基础知识点,理解并掌握核心概念和原理。 2. 学习编程技巧:熟练掌握至少一种编程语言,例如C++、Java等,并了解常用的编程工具和调技巧,提高编程能力。 3. 解题经验积累:多做一些编程题和算法题,提高解题能力和编程实践经验。可以通过参加一些线上或线下的编程竞赛来提升自己的算法和编程水平。 4. 多做模拟:通过模拟,熟悉的形式和题目类型,提前感受的压力和难度,并对自己的不足进行总结和改进。 总之,ECNU计科考研复试是对考生计算机基础知识和编程实践能力的综合考查,需要考生充分准备和深入理解相关知识点。只有全面提高自己的计算机科学水平,才能在中取得好成绩。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东部欧安时

祝你今天也顺利~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值