c++面试题

来源:互联网 发布:a标签 js单击事件 编辑:程序博客网 时间:2024/05/20 20:46

1、冒泡排序

#include<iostream>using namespace std;void bubbleSort(int a[],int length){cout<<"排序前"<<endl;for (int i = 0; i < length; i++){cout<<a[i]<<" ";}cout<<endl;for(int i=length; i>1; i--){for (int j=0; j < i-1; j++){if (a[j] > a[j+1]){int temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}cout<<"排序后"<<endl;for (int i = 0; i < length; i++){cout<<a[i]<<" ";}}int main(){int a[] = {4,2,6,9,7,6,3,1,4 ,8,5,6,1};int length = sizeof(a) / sizeof(int);bubbleSort(a,length);cout<<endl;system("pause");return 0;}

2、交换两个数 不用第三个数

A=A+B;B=A-B;A=A-B;


3、c++字符串逆序

///////////////////////////////// c++ 实现字符串逆序////////////////////////////////#include<iostream>#include<string>using namespace std;int main(){string str = "asdf";string strr(str.rbegin(),str.rend());cout<<strr<<endl;system("pause");return 0;}

4、写一个算法计算一颗二叉树的深度

int getTreeDepth(BTNode *p){int LD,RD;if(p == NULL)return 0;else{LD = getTreeDepth(p->lchild);RD = getTreeDepth(p->rchild);return LD>RD?LD+1:RD+1;}}


0 0
原创粉丝点击