面试要点:积极主动,给自己多争取机会
群面
1.自我介绍+兴趣爱好+优缺点,注意会让某位同学说说其他任一位同学都有哪些兴趣爱好或者优点,在介绍时做详细记录。
2.小组讨论,排序
3.小组辩论,随便神侃呗,说话保证流畅,逻辑清晰。对方对我的表述进行攻击时,有时候会不冷静的对你的逻辑能力进行质疑,人身攻击等等。记住,不予理睬(他攻击我表示他已经输了),还击的话可以交给队友。起冲突一定要避免,这个很损伤在团队内的威信。
4.提问环节,你觉得你们这一组中你认为表现最好和表现最差的同学是哪两位?如果那位所谓最差的同学不同意你的观点并且说的有理有据,那么最后挂掉的就是你。
Ps:主要是考验团队意识,时间意识,表现积极但不要太爱出风头,一定要在规定时间内讨论出结果,不能起内讧。1.话不能说太多,言多必失。2.千万不能不说话,如果没有侃侃而谈的能力,还是回家吧。。。言,必精!3.要提出能引导讨论方向的意见,这个十分重要,这个就是teamwork ablility。当然既然提出了,就必须被大家接受,不能被否决,这个一定要果断,因为方向一般只能定一次。4.想办法弄到陈述人的角色,如果很好的做到了1和2,那自荐一般不会被人否决。5.必须一直提醒自己,什么是面试官要的,而哪些是不重要的。(ps:提醒大家时间,协调内部矛盾,这两个小行为很加分哦~)
如果被问到谁表现最差,想想怎么说
技术面
常见问题:
【项目】
1.你说说这个软件是怎么在不同机器上实现通信的?你用到的是什么协议?
2.你是通过什么平台发布的?
3.你是怎么发布这个软件的?
4.介绍一下你自己的项目?
【java】
1.那你说说什么叫重载,什么叫继承、多态.....?
【数据库】
1.说说查询,删除是什么语句?update有什么用?那写一个语句出来看看吧
【通讯】 1.3G、4G的带宽是多少?
2.光纤有哪些、放大器的原理
【职位理解】
1.你对技术服务是怎么理解的?
【计算机网络】
1.osi七层协议?TCP/IP的五层结构图?每层都有哪些协议?高频
2.TCP/IP传输协议中的IP报文包含哪些内容?
3.说说正反馈和负反馈。。。?
【C/C++】
1.内存溢出怎么理解,可以举个例子吗?
2.怎么避免头文件被多次包含编译?
3.你学习C/C++多长时间,strcpy函数的用法?
4.你简历上的MFC你是用于嵌入式开发还是应用软件开发?
5.自己有没有用MFC开发过什么软件?
Ps:坦诚一点,不会的就是不会,把面试官往自己擅长的方向去引导。要有自己核心知识面,这方面我应该好好地把项目相关的东西和涉及的知识总结一下。诚恳,不懂就不懂,别装。如果感觉很差的话,博得怜悯也是一种方法,当然这个比较有难度,掌握不好尺度就会2的不行
Boss面
1.说说昨天的面试自己收获了什么,这个问题谈完又问有什么要补充的没?
2.补充自我介绍,简历上写的不用说,补充完又问我们还有没有要补充的?
3.自己谈下项目经历?
4.能保研为什么不读,读研不好吗?
5.谈谈自己姓名的蕴意;
6.各自谈谈家庭情况;
7.又问了是否有女朋友,开了下玩笑;
8.还有什么刚才没说的要补充的;
9.还有什么问题要问的?
a.“技术服务的发展前景、职业发展路线是怎样?”他说这去问你们师兄师姐,我补充道“我有认识一些华为的师兄师姐不过都刚毕业不久,我想了解在华为工作好几年的那些师兄师姐的情况”,他立即回了一句“在学校肯定是有在华为工作的师兄师姐的,你连这点都没做好,你不适合做市场,好吧你这个问题到此为止”。我一时语塞,感觉自己挖了一个坑跳了进去,另外两位都表示没什么问题要问的。 b.b.我就问您怎么评价我或对我有什么建议(注:这不是我自己觉我面试的不错,让面试官评价我,我以前看过一篇别人的帖子也是这么问的,自己莫名其妙的学来了),这里就给我自己下了套,boss也没评价我,直接让我用三分钟介绍自己的优缺点,考,三分钟啊,其实上面聊天过程中就已经说过了,现在又让我说,回答的就很纠结。之后我也不提问题了就结束了,让我回来等待通知。
10.说说你的项目,项目是什么、项目组几个人、你在项目中什么角色具体做了什么、项目过程中遇到了什么困难,项目中有什么启发感悟?
11.说是班长,那么班长干什么,组织哪些具体活动,最大型的是什么?
12.说完之后就是简历上面的东西。
13.大学期间觉得最成功的事。最难熬的事。
Ps:1.自己少说话(在一个boss面前,一些自以为很有远见的想法,很独特的视角,很可能都会被boss一顿乱训。。。),2.让boss多说,如何让?提问呗。当然所提的问题一定要有准备,要非常有质量,这样boss说的也会很high。3.说说自己对华为公司的了解,当然是好的方面。。。(这不是拍马屁,不是拍马屁,是拍马屁,拍马屁,马屁,屁。。。)
英语机试
1.对着机器朗读五个句子,难度递增;
2.四级水平的听力选择题10道;
3.两个英语topic,系统随机抽取,每个给20秒时间思考,然后用2分钟用英语讲。
a.where do you like to swim?how often?why?
b.Do you think the traffic will be better or worse in the future?why?
c.concert hall
D.对小孩玩网络的看法
性格测试
总结建议
1.网申的时候一定要重视自己写的每一个项目,每一句话,千万别太大意,因为最后放在面试官面前的就是你网申时候写的那些东西。
2.一面有不会的就说不会,引导面试官尽量往自己会的方向去说,想好针对简历项目面试官都会问些什么牛角尖问题。
3.二面随机应变,不在说的多,而在说的有理有据,让别人印象深刻,平时可以上网找找相关的群面题目,与同学进行探讨。
4.性格测试不要用太多时间思考推敲,凭直觉做就行了,即使觉得有可能前后矛盾也果断选上去吧,听说这部分同时也是在测谎。
5.终面问的问题都很随机,每组进去被问到的都不太一样。可以适当多说,但也要避免话多必失;当然不管是否被刁难都表现得镇定一点,不必急躁、争的面红耳赤。
6.面试环节提问部分一定要看面试官的档次,对于普通的HR提一些无关紧要的问题没关系,但是对于boss级的提问一定要提的有深度点,提问前最好已经有了自己的见解或者解决方案,因为他一般都不会直接回答你的问题。
7.英语topic机试,不要盯着电脑屏幕看时间一秒秒的过去,这样只能更紧张,专心组织好语言,不要停顿太长时间,尽量说到时间结束,当然这玩意儿还是要靠平时多读一读,培养语感才是王道,面外企是必须的。
8.华为的面试不会太看中硬实力(如成绩,获奖什么的),更为看重的是表达、逻辑、抗压能力,主动性和团队意识等,当然硬实力好也是没错的。 9.面试前,要有最基本的准备:穿着要正式点,自我介绍、自己的优缺点举几个、对公司文化的了解、对你面试职位的理解,以及具体的面试流程是什么;
10.一定要注意时间,提前到面试地点;
11.做到积极主动,尽量给自己争取机会
12.最后有什么问题问,不要问面试官怎么评价自己,面试很避讳这个。
13.感觉技术服务岗和技术岗在技术面时重点不一样,技术服务偏通信基础知识,没什么技术含量,而且填技术服务岗的本科生比较多。
华为可以用六个月的时间通过魔鬼训练把一个法学院的学生训练成网络高手
第二篇:华为机试题目总结(程序篇)
自己写的,水平很挫,仅供参考
目录
1.语言识别问题
2.销售网络问题(未完成)
3.股票投资问题
4.判断手机号码合法性
5.元音字母复制
6.验证身份证号
7.选秀节目打分
8.数组最大值放中间,其他依次放其左右(规律未找着,未完成)
9.任务调度(解题关键,需要一个容器来承载下标跟值的一一对应关系,最好就是定义一个结构体)
10.将某字符变成小写后的某个字符
11.链表的逆序
12.单词统计
13.字符串进行转换,转换成相应的数字已知:yi er san si wu liu qi ba jiu 分别对应,对一段只含有这几种字符的字符串进行转换,转换成相应的数字
14.一个数组中比平均数大的个数
15.求一个数组中第一大和第二大数
16.字符变成整数
17.整数变字符
18.判断素数问题
19(1).约瑟夫环(循环列表)
19(2).约瑟夫环(数学方法只能求出最后的胜利者的序号)
19(3).约瑟夫环(容器实现)
20.判断某个整数是回文。即这样的,反过来还是
21.判断一个字符串是不是回文
22.求一个字符串中的最大回文子串,就是从n个字符开始检查是不是回文,知道m个字符符合回文,那么这个就是最大回文
23.找出^n的数
24.统计一个数二进制表达中的个数
25.镜像反转二进制表达式,并输出十进制值
26.连续字符统计
27.判断一个字符串中()是否配对
28.查找子字符串个数
29(1).找出一个字符串中是否包含相同(包括连续的)的子字符串(要求子串长度大于等于)并输出出现频率最高的子字符串
29(2)找出一个字符串中是否包含连续相同的子字符串,并输出出现频率最高的子字符串
30.删除字符窜中字符数最少的字符
31.关于数组的循环移位,左移为负,右移为正
32.求一个二维数组每列的最小值
33.两个字符串,求最长公共子串
34.超大整数加法运算,大整数会用字符串或者数组来存,不过注意低位存字符前面几位,高位存后面,存到字符中应该存“”。这边我用的是数组
35.排序总结
36.将一个字符串空格分隔,并倒序输出
37.删除一个字符串中的某个字符串
38.取出一个字符串中所有的数字,并取出所有字母 39,简单的字符统计
40.查找字符串中空格分隔的单词的最大长度
41.二叉树的操作
42.分块查找
1.语言识别问题
#include <iostream>
using namespace std;
void main()
{
int n,S_num=0,T_num=0,m=0;
cin>>n;
char ch;
getchar();
for(int i=0;i<n;i++)
{ // m=0;
while(1)
{ ch=getchar();
/* m++;
if((m>3)&& (ch=='\n'))
{
m=0;
break;
}*/
if(ch=='\n') break;
if(ch=='s'||ch=='S') S_num++; if(ch=='t'||ch=='T') T_num++;
}
}
if(S_num<T_num) cout<<"English\n"; else cout<<"Deutsch\n";
}
2.销售网络问题(未完成)
#include <iostream>
using namespace std;
void main()
{
int n,S_num=0;
cin>>n;
int a[n];
for(int i=0;i<n-1;i++)
cin>>a[i];
if(a[])
for(int i=0;i<n;i++)
{ // m=0;
while(1)
{ ch=getchar(); /* m++;
if((m>3)&& (ch=='\n')) {
m=0;
break;
}*/
if(ch=='\n') break;
if(ch=='s'||ch=='S') S_num++; if(ch=='t'||ch=='T') T_num++;
}
}
if(S_num<T_num) cout<<"English\n"; else cout<<"Deutsch\n"; }
3.股票投资问题 #include <iostream>
using namespace std;
void main()
{
int B,C=0,D=0,E=0,i,j,k,l,n,m; int A;
int a[12];//未来天得股价 int b[12][12];
cin>>B;//测试数
memset(b,0,sizeof(b)); //for(i=0;i<B;i++)
cin>>A;
for(j=0;j<12;j++)
cin>>a[j];
int temp=0;
for(k=0;k<11;k++)
for(l=k+1;l<12;l++)
{
temp=A/a[k];
b[k][l]=temp*(a[l]-a[k]); if(b[k][l]<0)
b[k][l]=0;
}
int max=b[0][1];
m=0;
n=1;
for(k=0;k<11;k++)
for(l=k+1;l<12;l++)
{
if(b[k][l]>max)
{ max=b[k][l];
m=k;
n=l;
}
if(b[k][l]==max)//相等的取购价低的 { if(a[k]<a[m])
{ max=b[k][l];
m=k;
n=l;
}
}
}
if (max==0)
{cout<<"IMPOSSIBLE"<<endl; }
else{
C=m+1;
D=n+1;
E=max;
cout<<C<<" "<<D<<" "<<E<<endl;
}
}
4.判断手机号码合法性 #include <iostream>
using namespace std;
int verifyMsisdn(char* inMsisdn)
{
int n=0;
int i=0;
int j=0;
char *p;
p=inMsisdn;
while(p[i]!='\0')
{ i++;
n++;
}
if(n!=13)
return 1;
else
{
while(p[j]!='\0')
{
if(!((p[j]>='0' && p[j]<='9')))
{return 2;
break;
}
j++;
}
if(!(p[0]=='8'&& p[1]=='6'))
return 3;
else return 0;
}
}
void main()
{
char a[20];
cin>>a;
int m=verifyMsisdn(a);
cout<<m;
}
5.元音字母复制
#include <iostream>
using namespace std;
void sortVowel (char* input)
{ int j=0;
char output[50]={0};
for(int i=0;input[i]!='\0';i++)
{ if(input[i]=='a' || input[i]=='e'|| input[i]=='i'|| input[i]=='o'|| input[i]=='u') {
output[j]=input[i];
j++;
}
}
int w=j;
char temp;
for(int k=0;k<j-1;k++)
for(int l=0;l<j-1-k;l++)
{
if(output[l]>output[l+1])
{
temp=output[l];
output[l]=output[l+1];
output[l+1]=temp;
}
}
for(int i=0;input[i]!=0;i++)
{ if( input[i]=='A'|| input[i]=='E'|| input[i]=='I'|| input[i]=='O'|| input[i]=='U') {
output[j]=input[i];
j++;
}
}
char temp2;
for(int m=w;m<j-1;m++) for(int n=w;n<j-1-m;n++) {
if(output[n]>output[n+1]) {
temp2=output[n];
output[n]=output[n+1]; output[n+1]=temp2;
}
}
cout<<output;
}
void main()
{
char a[50];
cin.get(a,50);
sortVowel(a);
}
6.验证身份证号 #include <iostream>
using namespace std;
int verifyIDCard(char* input)
{
int n=0;
int i=0;
int j=0;
char *p;
p=input;
while(p[i]!='\0')
{ i++;
n++;
}
if(n!=18)
return 1;
else
{
while(j<17)
{
if(!((p[j]>='0' && p[j]<='9')))
{return 2;
break;
}
j++;
}
if(!(p[17]=='x'|| (p[17]>='0' && p[17]<='9')))
return 3;
else
{
int year=(p[6]-'0')*1000+(p[7]-'0')*100+(p[8]-'0')*10+(p[9]-'0');
int month=(p[10]-'0')*10+(p[11]-'0');
int day=(p[12]-'0')*10+(p[13]-'0');
if(!(year>=1900 && year<=2100))
return 4;
else{
if(!(month>=1 && month<=12))
return 5;
else{
//能被整除且不能被整除或能被整除的年份
bool ryear=(year%4==0 && year%100!=0) || (year%400==0);
if(!((!ryear && day>0 && day<29)|| (ryear && day>0 && day<=29))) return 6;
else{
return 0;
}
}
}
}
}
}
void main()
{
for(int c=0;c<10;c++)
{char a[20];
cin>>a;
int m=verifyIDCard(a);
cout<<m;
}
}
7.选秀节目打分
#include <iostream>
using namespace std;
#define N 5
int total(int score[],int judge_type[],int cn)
{ int sum1=0,sum2=0,m=0,n=0,aver=0,totalscore=0;
for(int i=0;i<cn;i++)
{ if(judge_type[i]=1)
{sum1+=score[i];
m++;
}
else
{
sum2+=score[i];
n++;
}
}
if(n==0)
totalscore=sum1/m;
else
{ totalscore=(int)(sum1/m * 0.6+sum2/n * 0.4);}
return totalscore;
}
void main()
{
int score[N];
int judge_type[N];
for(int i=0;i<N;i++)
{ cout<<"输入第"<<i+1<<"个评委的类别"<<endl;
cin>>judge_type[i];
cout<<"输入第"<<i+1<<"个评委的分数"<<endl;
cin>>score[i];
}
int totalscore= total(score,judge_type,N);
cout<<totalscore<<endl;
}
8.数组最大值放中间,其他依次放其左右(规律未找着,未完成)
#include<iostream>
using namespace std;
void sort(int input[], int n, int output[])
{
int i,j;
int temp =0;
for(i =0; i<n-1; i++)
for(j =0; j<n-i-1; j++)
{
if(input[j]>input[j+1])
{
temp = input[j];
input[j] = input[j+1]; input[j+1] = temp;
}
}
if(n%2 ==0)
{
for(i =0 ; i<n/2; i++)
{
output[i] = input[2*i];
}
for(i =0; i<n/2; i++)
{
output[n/2+i] = input[n-1-2*i]; }
}
else
{
for(i=0; i<(n-1)/2; i++)
{
output[i] = input[2*i+1]; }
output[(n-1)/2]= input[n-1];
for(i = 0; i<(n-1)/2; i++)
{
output[(n-1)/2+1+i] = input[n-3-2*i]; }
}
for(i = 0 ; i<n; i++)
{
printf("%d", output[i]);
}
printf("\n");
}
int main()
{
int input1[] = {3, 6, 1, 9, 7};
int input2[] = {3, 6, 1, 9, 7, 8};
int output1[5] = {0};
int output2[6] = {0};
sort( input1, 5,output1) ;
sort(input2, 6, output2) ;
}
9.任务调度
(解题关键,需要一个容器来承载下标跟值的一一对应关系,最好就是定义一个结构体) #include<iostream>
using namespace std;
struct table
{ int number;
int value;
};
void scheduler(int task[], int system_task[], int user_task[],int n)
{
struct table *sb=(struct table *)malloc(n*sizeof(struct table));
for(int i=0; i<n; i++)
{ sb[i].number=i;
sb[i].value=task[i];
}
struct table temp;
for(int k=0; k<n-1; k++)
for(int j=0; j<n-1-k; j++)
{
if(sb[j].value>sb[j+1].value)
{
temp=sb[j];
sb[j]= sb[j+1];
sb[j+1] = temp;
}
}
int cs=0,cu=0;
for(int l=0; l<n; l++)
{
if(sb[l].value<50)
{system_task[cs]=sb[l].number;
cs++;
}
else if(sb[l].value<=255)
{user_task[cu]=sb[l].number;
cu++;
}
else
continue;
}
system_task[cs]=-1;
user_task[cu]=-1;
free(sb);
for(int m=0;m<=cs;m++)
{
cout<<system_task[m];
}
printf("\n");
for(int n=0;n<=cu;n++)
{
cout<<user_task[n];
}
}
int main()
{
int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99}; int n=9;
int count_sys=0,count_user=0;
for(int i=0;i<9;i++)
{
if(task[i]<50)
count_sys++;
else if(task[i]<=255)
count_user++;
else
continue;
}
int *system_task=(int *)malloc(count_sys*sizeof(int)+4); int *user_task=(int *)malloc(count_user*sizeof(int)+4);
scheduler(task, system_task, user_task,9);
//int *p = system_task;
//int *q = user_task;
//
////printf("%d%d\n", count_sys,count_user);
// for(int i=0;i<count_sys+1;i++)
// {
// printf("%d", system_task[i]);
}
// } // printf("\n"); // // for(int i=0;i<count_user+1;i++) // { // printf("%d", user_task[i]); // } free(system_task); free(user_task);
10.将某字符变成小写后的某个字符 #include<iostream>
using namespace std;
void TransferString(const char * pInputStr, long lInputLen, char * pOutputStr) {
for(int i=0;i<lInputLen;i++)
{
if(pInputStr[i]>='V'&& pInputStr[i]<='Z')
pOutputStr[i]=pInputStr[i]+11;//('a' - 'A')-('V' - 'A');
else if (pInputStr[i]>='A'&& pInputStr[i]<='U')
pOutputStr[i]=pInputStr[i]+37;//('a' - 'A')+('F' - 'A');
else{pOutputStr[i]=pInputStr[i];}
}
cout<<pOutputStr;
}
void main()
{
char *pInputStr="Axs3mWss";
int n=0;
while(pInputStr[n]!='\0')
n++;
long lInputLen=n+1;
char *pOutputStr=(char *)malloc(sizeof(char)*(n+1));
TransferString(pInputStr,lInputLen,pOutputStr);
}
11.链表的逆序 #include<iostream>
using namespace std;
typedef struct tagListNode
{
int value;
struct tagListNode *next;
}ListNode;
//要求实现函数:
void converse(ListNode *head) {
ListNode *p1,*p2,*p3;
p1=head;
p2=p1->next;
while(p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
head=p1;
while(p1!=NULL)
{
cout<<p1->value<<"->"; p1=p1->next;
}
}
void main()
{
ListNode *p,*head,*s;
head=(ListNode*)malloc(sizeof(ListNode));
p=head;
int n=0,m=0;
while(n<5)
{
cin>>m;
s=(ListNode*)malloc(sizeof(ListNode)); s->value=m;
p->next=s;
p=s;
n++;
}
head=head->next;
p->next=NULL;
converse(head);
//p=head;
//while(p!=NULL)
// {
// cout<<p->value<<"->";
// p=p->next;
//
// }
}
12.单词统计
#include<iostream>
#include<string>
using namespace std;
struct node
{
//int number;
int count;
char a[10];
};
void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord) {
//cout<<sizeof(pOutputHotWord)<<endl;
int i=0;
char *pInputStr1=(char *)malloc(100*sizeof(char));
strcpy(pInputStr1,pInputStr);//(char *)
while(pInputStr1[i]!='\0')
{
if(pInputStr1[i]>='A' && pInputStr1[i]<='Z')
pInputStr1[i]+=32;
i++;
}
const char * split = ", .";
struct node sb[10]={0};//*sb=(struct node *)malloc(10*sizeof(struct node)); char *p={0};
p=strtok (pInputStr1,split);
int j=0;
while(p!=NULL)
{ //sb[j].number=j;
strcpy(sb[j].a,p);
sb[j].count=0;
j++;
p=strtok (NULL,split);
}
for(int k=0;k<10;k++)
for(int l=0;l<10;l++)
{
if (strcmp(sb[k].a,sb[l].a)==0)
sb[k].count+=1;
}
struct node max;
struct node min;
int dex1=0,dex2=0;
max=sb[0];
min=sb[0];
for(int m=0;m<j;m++)
{
if(sb[m].count>max.count)
{ max=sb[m];
dex1=m;}
else if((sb[m].count<min.count) &&(sb[m].count!=min.count) ) {
min=sb[m];
dex2=m;}
}
/*for(int m=0;m<j;m++)
{
cout<<sb[m].count<<endl;
cout<<sb[m].a;
}*/
strcpy(pOutputHotWord,sb[dex1].a);
strcpy(pOutputColdWord,sb[dex2].a);
cout<<"最高"<<pOutputHotWord;
cout<<"最低"<<pOutputColdWord;
}
void main()
{
char pInputStr[100]={0};
cin.get(pInputStr,100);
char * pOutputHotWord=( char *)malloc(sizeof( char *)*100); char * pOutputColdWord=( char *)malloc(sizeof( char *)*100); memset(pOutputHotWord, 0, sizeof(pOutputHotWord)) ; memset(pOutputColdWord, 0, sizeof(pOutputHotWord)) ; WordStat(pInputStr, pOutputHotWord,pOutputColdWord);
}
13.字符串转换成规定数字
转换成相应的数字已知:yi er san si wu liu qi ba jiu 分别对应,对一段只含有这几种字符的字符串进行转换,转换成相应的数字
如:yiersansan:
#include<iostream>
#include<string>
using namespace std;
int WordStat(const char * pInputStr, char * pOutputWord)
{
int i=0,d=0,k=0,sum=0;
char *pInputStr1=(char *)malloc(100*sizeof(char));
strcpy(pInputStr1,pInputStr);//(char *)
char* sss[9] = {"yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
while(pInputStr1[i]!='\0')
{
if(pInputStr1[i]=='y' || pInputStr1[i]=='e'|| pInputStr1[i]=='w'|| pInputStr1[i]=='q'|| pInputStr1[i]=='b')
d=2;
if(pInputStr1[i]=='l' || pInputStr1[i]=='j')
d=3;
if(pInputStr1[i]=='s')
{ if(pInputStr1[i+1]=='a')
d=3;
if(pInputStr1[i+1]=='i')
d=2;
}
for(int j=0;j<9;j++)
{
if(strncmp(pInputStr1+i,sss[j],d)==0)
k=j+1;
}
sum=sum*10+k;
i+=d;
}
return sum;
}
void main()
{
char pInputStr[100]={0};
cin.get(pInputStr,100);
char * pOutputWord=( char *)malloc(sizeof( char *)*100);
memset(pOutputWord, 0, sizeof(pOutputWord)) ;
int transver= WordStat(pInputStr, pOutputWord);
cout<<transver;
}
14.一个数组中比平均数大的个数 #include<iostream>
#include<string>
using namespace std;
int count(int p[], int n)
{
int sum=0,m=0;
for(int i=0;i<n;i++)
{
sum+=p[i];
}
int aver=sum/n;
for(int j=0;j<n;j++)
{
if(p[j]>aver)
m++;
}
return m;
}
void main()
{
cout<<"输入个数n"<<endl;
int n;
cin>>n;
int *a=(int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int m=count(a,n);
cout<<m;
}
15. 求一个数组中第一大和第二大数
#include<iostream>
#include<string>
using namespace std;
void count(int p[], int n)
{
int max=0,smax=0,k=0;
for(int i=0;i<n;i++)
{
if(p[i]>max)
{ max=p[i];
k=i;
}
}
for(int j=0;j<n ;j++)
{if(j==k)continue;
if(p[j]>smax)
smax=p[j];
}
cout<<"最大"<<max<<endl;
cout<<"二大"<<smax<<endl;
}
void main()
{
cout<<"输入个数n"<<endl;
int n;
cin>>n;
int *a=(int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
count(a,n);
}
16.字符变成整数
#include<iostream>
#include<string>
using namespace std;
int chartoint(const char * pInputStr) {
int i=0,d=0,k=0,sum=0;
while (pInputStr[i]!='\0') {
d=pInputStr[i]-'0';
sum=sum*10+d;
i++;
}
return sum;
}
void main()
{
char pInputStr[100]={0}; cin.get(pInputStr,100);
int transver= chartoint(pInputStr);
cout<<transver+1;
}
17.整数变字符
#include<iostream>
using namespace std;
void main()
{
int n,i=0;
cin>>n;
//int m=(int)sqrt(n);
char p[50]={0},s[50]={0};
while(n)
{
p[i]=n%10+'0';
i++;
n/=10;
}
p[i]='\0';
int m=strlen(p);
//char *s=(char *)malloc(sizeof(char)*(m+1));
for (int j = 0; j < m; j ++)
s[j]=p[m-1-j];
s[m]='\0';
cout<<s;
}
18.判断素数问题
#include<iostream> #include<math.h> using namespace std;
void main()
{
int n,i=0;
cin>>n;
//int m=(int)sqrt(n); for(i=2;i<n;i++) {
if(n%i==0) break;
}
if(i==n)
cout<<"sushu"<<endl; else{cout<<"bushisushu"<<endl;}
}
19.1约瑟夫环 #include<iostream> using namespace std; typedef struct LNode {
int data;
struct LNode*link; }LNode;
void jos(int n,int k,int m) {
LNode *p,*curr,*r;
p=(LNode*)malloc(sizeof(LNode)); p->data=1;//注意我是从开始的奥
p->link=p;
curr=p;
for(int i=2;i<=n;i++)
{
LNode *s=(LNode*)malloc(sizeof(LNode));
s->data=i;
s->link=curr->link;
curr->link=s;
curr=s;
}//==============================================循环链表的建立
while(--k)
{ r=p;
p=p->link;
}//======================p指向序号为k的位置
int w=m;
while(n--)
{
while(--m)
{r=p;
p=p->link;
}//======================p指向序号为k的之后的m位置上
cout<<p->data<<"->";
r->link=p->link;
p=r->link;
m=w;
}
}
void main()
{
jos(9,1,5);
}
//5->1->7->4->3->6->9->2->8->
19.2约瑟夫环(数学方法只能求出最后的胜利者的序号)
#include<stdio.h>
int main()
{
int n,m,i,s = 0;
printf("N M =");
scanf("%d%d",&n,&m);
for(i = 2; i <= n; i++)
{
s = (s + m) % i;
}
printf("n The winner is %dn",s+1);
}
19.3约瑟夫环(容器实现)
#include<vector>
#include<iostream>
using namespace std;
const int N = 9;
const int M = 5;
const int k = 1;
int main(int argc, char* argv[])
{
vector<int> ring;
for(int i=0; i<N;i++)
ring.push_back(i+1);
vector<int>::iterator iBegin = ring.begin();
vector<int>::iterator iEnd;
while ( !ring.empty() )
{
iEnd = ring.end();
if(iBegin == iEnd )
iBegin = ring.begin();
for(int i=1;i<M;i++)
{
iBegin++;
if(iBegin >= iEnd)
iBegin = ring.begin();
}
cout<<*iBegin<<endl;
iBegin = ring.erase(iBegin);
}
}
20.判断某个整数是回文。即这样的,反过来还是
#include<iostream>
using namespace std;
bool func(int m);
void main()
{
int m;
cout<<"enter a number:"<<endl;
cin>>m;
cout<<func(m)<<endl;
}
bool func(int m)
{
int i,n=0;
i=m;
while(i)
{
n=n*10+i%10;
i/=10;
}
if(m==n)
return true;
return false;
}
21.判断一个字符串是不是回文 #include<iostream>
using namespace std;
#include<string.h>
bool is_huiwen(char a[],int length)
{
const char *src=a;
const char *end;
end=src+length-1;
while(src<end)
{ if(*src==*end)
{ src++;end--;}
else return false;
}
return true;
}
int main()
{ int len;
char c[10];
cout<<"enter:"<<endl;
cin>>c;
len=strlen(c);
bool h;
h=is_huiwen(c,len);
if(h) cout<<"hui_wen"<<endl;
else cout<<"non_hui_wen"<<endl;
return 0;
}
22.求一个字符串中的最大回文子串
就是从n个字符开始检查是不是回文,知道m个字符符合回文,那么这个就是最大回文
#include<iostream>
using namespace std;
#include<string.h>
char * maxhuiwen(char a[],int length,char b[])
{
int i=0,j=0,k=0;
for(i=length;i>0;i--)//回文的长度
for( j=0;j<=length-i;j++)//这个其实假设的回文开始字符位置 {
for( k=0;j+k<j+i-1-k;k++)
{
if(a[j+k]==a[j+i-1-k])
continue;
else{break;}
}
if(j+k>=j+i-1-k)
{
int n1=i;//长度
int n2=j;//起始位置
cout<<n1<<endl;
cout<<n2<<endl;
memcpy(b,a+j,i);
b[i]='\0';
return b;
}
}
}
void main()
{ int len;
char c[50];
cout<<"enter:"<<endl;
cin>>c;
len=strlen(c);
char * output={0};
output =(char *)malloc(sizeof(char)*(len+1));
char *s={0};
s=maxhuiwen(c,len,output);
cout<<s;
}
23.找出^n的数
#include<iostream>
using namespace std;
void func(int a[],int n)
{
for(int i=0;i<n;i++)
{
if(0==(a[i]&(a[i]-1)))
cout<<a[i]<<endl;
}
}
void main()
{
int a[5];
for(int i=0;i<5;i++)
cin>>a[i];
int n=sizeof(a)/sizeof(int);
cout<<n<<endl;
func(a,5);
}
24.统计一个数二进制表达中的个数 #include<iostream>
using namespace std;
int func(int a)
{
int c=0,i=0;
while(a)
{
if((a & 1)==0)
{c++;}
/* i++;*/
a=a>>1;
}
return c;
}
void main()
{
int a;
cin>>a;
int m=func(a);
cout<<m<<endl;
}
25.镜像反转二进制表达式,并输出十进制值#include<iostream>
using namespace std;
int func(int a)
{
int c=0,i=0;
int b[32];
while(a)
{
b[i]=(a & 1);
i++;
a=a>>1;
}
for(int j=0;j<i;j++)
c=c*2+b[j];
return c;
}
void main()
{
int a;
cin>>a;
int m=func(a);
cout<<m<<endl;
}
26.连续字符统计
#include<iostream>
using namespace std;
#include<string.h>
#include<iostream>
using namespace std;
#include<string.h>
char * maxcommonchar(char a[],int length) {
int n=1;
char *p=(char *)malloc(sizeof(char)*(length*2)); memset(p,0,sizeof(p));//需要初始化
for(int i=0;i<length;i++)
{
if (a[i]==a[i+1])
{n++;}
else
{
sprintf(p+strlen(p),"%c%d",a[i],n);
n=1;
}
}
return p;
}
void main()
{
char *a="assddff";
int n=strlen(a);
cout<<maxcommonchar(a,n);
}
27.判断一个字符串中()是否配对 #include<iostream>
using namespace std;
bool match(char a[],int length);
int main()
{
char b[100];
int len;
bool m;
cout<<"enter:"<<endl;
gets(b);
len=strlen(b);
m=match(b,len);
if(m) cout<<"match"<<endl;
else cout<<"nonmatch"<<endl;
return 0;
}
bool match(char a[],int length)
{
char *p=a;
int count1=0;
int count2=0;
while(*p!='\0')
{
if(*p=='(') count1++;
if(*p==')') count2++;
if(count2>count1)
return false;
p++;
}
if(count1==count2)
return true;
else
return false;
}
28.查找子字符串个数
#include<iostream>
using namespace std;
int fun(char a[],char b[])
{
int n=0;
int n1=strlen(a);
int n2=strlen(b);
for(int i=0;i<=n1-n2;i++)
{if(strncmp(a+i,b,n2)==0)
{ n++;}
}
return n;
}
void main()
{
char a[100],b[100];
cin>>a;
cin>>b;
int n=fun(a, b);
cout<<n;
}
29.1找出一个字符串中是否包含相同(不管是不是连续的)的子字符串
(要求子串长度大于等于)并输出出现频率最高的子字符串
#include<iostream> using namespace std; void fun(char a[])
{
int n=strlen(a); int m=0;
char b[100];
int p=0;
int s1[10],s2[10],s3[10]; for(int i=2;i<=n/2;i++) for(int j=0;j<=n-i;j++)
{ strncpy(b,a+j,i);
b[i]='\0';
for(int k=0;k<n-j-1;k++) {
if(strncmp(b,a+j+k,i)==0) {
m++;
}
}
if(m>=2)
{s1[p]=m;
s2[p]=j;
s3[p]=i;
p++;
}
m=0;
}
int max=0;
int l=0;
for(int q=0;q<p;q++) if(s1[q]>max) { max=s1[q]; l=q;
}
for(int o=0;o<s3[l];o++) cout<<a[s2[l]+o];
}
void main()
{
char a[100];
cin>>a;
fun(a);
/*if(fun(a))
{cout<<"you";}
else{cout<<"meiyou";}*/
}
29.2找出一个字符串中是否包含相同并且连续的子字符串,并输出出现频率最高的子字符串
#include<iostream>
#include<string.h>
using namespace std;
void fun(char a[])
{
int n=strlen(a);
char *s[100];
for(int n3=0;n3<100;n3++)
{
s[n3]=(char *)malloc(20*sizeof(char));
}
for(int n1=0;n1<n;n1++)
strcpy(s[n1],a+n1);
/*for(int n2=0;n2<n;n2++)
cout<<s[n2]<<endl;*/
int c=1;
int max=0;
int m=0;
int p;
for(int i=0;i<n;i++)//第一个字符串
for(int j=i+1;j<n;j++)//与上面字符串相比较的字符串
{ if(strncmp(s[i],s[j],j-i)==0)
{ c++;
for(int k=j+j-i;k<n;k++)
{
if(strncmp(s[i],s[k],j-i)==0)
{
c++;
}
else{break;}
}
if(c>max)
{ max=c;
m=i;
p=j;
}
}
}
for(int o=0;o<p-m;o++)
cout<<*(s[m]+o);
}
void main()
{
char a[100];
cin>>a;
fun(a);
/*if(fun(a))
{cout<<"you";}
else{cout<<"meiyou";}*/
}
30.1删除字符窜中字符数最少的字符 #include<iostream>
#include<string>
using namespace std;
struct node
{
int count;
char a;
};
char *minWorddelete( char * pInputStr, char * pOutputWord)
{ int n=strlen(pInputStr);
struct node sb[100]={0};//*sb=(struct node *)malloc(10*sizeof(struct node));
for(int i=0;i<n;i++)
{
sb[i].a=pInputStr[i];
sb[i].count=0;
}
for(int k=0;k<n;k++)
for(int l=0;l<n;l++)
{
if (sb[k].a==sb[l].a)
sb[k].count+=1;
}
struct node min;
int dex1=0;
min=sb[0];
int m=0;
for(m=0;m<n;m++)
{
if(sb[m].count<min.count)
{
min=sb[m];
dex1=m;
}
}
cout<<"删除的字符是"<<sb[dex1].a<<endl;
int q=0;
for(int p=0;p<n;p++)
{
if(sb[p].a!=sb[dex1].a)
{
pOutputWord[q]=sb[p].a;
q++;
}
}
pOutputWord[q]='\0';
return pOutputWord;
}
void main()
{
char pInputStr[100]={0};
cin.get(pInputStr,100);
char * pOutputWord=( char *)malloc(sizeof( char *)*100);
memset(pOutputWord, 0, sizeof(pOutputWord)) ;
char *s=minWorddelete(pInputStr, pOutputWord);
cout<<s;
}
30.2简单的字符统计的,但是效率比较低 #include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
void main()
{int a[128];
memset(a,0,sizeof(a));
string s="gdssdsgs";
int n=s.size();
int i = 0;
for ( i = 0; i<n; i++ )
{ int p=s[i];
a[p]+=1;}
for (int j = 0;j<128; j++ )
{if(a[j]>0 )
cout<<(char)j<<":"<<a[j];
}
}
31.关于数组的循环移位,左移为负,右移为正 #include<iostream>
using namespace std;
void cycleDisplacement(int *a, int n, int k)
{
int temp=0;
if(k>0)
{while(k)
{temp=a[n-1];
for(int j=n-1;j>0;j--)//注意右移覆盖要从大的那边开始覆盖,即j-- {a[j]=a[j-1];
}
a[0]=temp;
k--;
}
}
else if(k<0)
{while(k)
{temp=a[0];
for(int j=0;j<n-1;j++)//注意左移覆盖要从小的那边开始覆盖,即j++ {a[j]=a[j+1];
}
a[n-1]=temp;
k++;
}
}
}
void main()
{
int a[]={1,2,3,4,5};
int n=5;
cycleDisplacement(a,5,-2);
for(int i=0;i<5;i++)
{
cout<<a[i];
}
}
32.求一个二维数组每列的最小值 #include<iostream>
using namespace std;
void arraymin(int input[][4],int n1,int n2,int output[])
{
for(int i=0;i<n2;i++)
{ output[i]=input[0][i];
for(int j=0;j<n1;j++)
{
if(input[j][i]<output[i])
output[i]=input[j][i];
}
}
}
void main()
{
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int n1=3;
int n2=4;
int *output=(int *)malloc(sizeof(int)*n2);
arraymin(a,n1,n2,output);
cout<<"原二维数组是:"<<endl;
for(int i=0; i<3; i++)
{
for(int j=0; j<4; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"每列最小值是:"<<endl;
for(int k=0;k<n2;k++)
cout<<output[k]<<endl;
}
33.1两个字符串,求最长公共子串 #include<iostream>
using namespace std;
#include<string.h>
char * maxcommonchar(char a[],char b[],char c[])
{
int n1=strlen(a);
int n2=strlen(b);
int n=(n1>=n2) ? n2:n1;
//cout<<n;
int i=0,j=0,k=0,l=0;
for( i=n;i>=2;i--)//最大子串长度
for( j=0;j<n1-i;j++)//a起始位置
for( k=0;k<n2-i;k++)//b起始位置
{
if(strncmp(a+j,b+k,i)==0)
{
for(l=0;l<i;l++)
{
c[l]=a[j+l];
}
c[l]='\0';
// cout<<c;
return c;
}
}
}
void main()
{
/*char *a="fdfdsfdsf";
char *b="sdfdf";*/
char a[100];
char b[100];
cin>>a;
cin>>b;
char output[100]={0};
char *s={0};
s=maxcommonchar(a,b,output);
cout<<s;
}
从网上拷过来的程序,如下所示,他的复杂度比我的低。 #include<string.h>
#define M 100
//LCS问题就是求两个字符串最长公共子串的问题
char* LCS(char left[],char right[])
{
//获取左子串的长度,获取右子串的长度
int lenLeft=strlen(left),lenRight=strlen(right),k;
//注意这里要写成char型,而不是int型,否则输入整型数据时会产生错误。
//矩阵c纪录两串的匹配情况
//char *c=(char *)malloc(lenRight);
char *p;
int c[M][M]={0};//当将c申明为一个二维数组时
int start,end,len,i,j;//start表明最长公共子串的起始点,end表明最长公共子串的终止点 end=len=0;//len表示最长公共子串的长度
for(i=0; i<lenLeft; i++) //串从前向后比较
{
//串从后向前比较,为什么要从后向前呢?是把一维数组c[ ]当二维数组来用, //如果要从前向后,可以将c申明为一个二维数组c[M][M].但程序要做相应调整. for(j=0;j<lenRight;j++)//当c申明为一个二维数组时
//for(j=lenRight-1; j>=0; j--)
{
if(left[i] == right[j])//元素相等时
{
if(i==0||j==0)
//c[j]=1;
c[i][j]=1; //这边是是因为若是i,j有一个为,说明是从某一个字符串的开头开始的,这个是公共字符串的起点
else
{
//c[j]=c[j-1]+1;
c[i][j]=c[i-1][j-1]+1; //只有前面的字符也是相等的,这边的计数器才会加一,
}
}
else
//c[j] = 0;
c[i][j]=0;
//if(c[j] > len)
if (c[i][j]>len)
{
//len=c[j];
len=c[i][j];
end=j;
}
}
}
start=end-len+1;
//数组p纪录最长公共子串
p =(char*)malloc(len+1);
for(i=start; i<=end; i++)
{
p[i-start] = right[i];
}
p[len]='\0';
return p;
}
void main()
{
char str1[M],str2[M];
printf("请输入字符串:");
gets(str1) ;
printf("请输入字符串:");
gets(str2);
printf("最长子串为:");
printf("%s\n",LCS(str1,str2));
}
33.2 n个字符串的最大公共子串
需要调用子函数的
#include <iostream>
using namespace std;
char * maxchar(const char * s[],char * p,int n)
{
int j=0,k=0,l=0,m=0;
for(j=strlen(s[0]);j>0;j--)//最大字符串的长度
for(k=0;k+j-1<strlen(s[0]);k++)//最大字符串开始的位置,注意第二个字符的位置k+1. {int flags1=1;
for(l=1;l<n;l++)
{int flags2=0;
for(m=0;m+j-1<strlen(s[l]);m++)
{
if(strncmp(s[0]+k,s[l]+m,j)==0)
{//cout<<"我是大笨蛋";
flags2=1;
break;
}
}
if(!flags2)//如果循环到这个地方有某个字符串中没有s[0]中挑出的那个字符串,说明这个字符串不是最大的,将代表最大字符串的标志位设为
{//cout<<"我是大笨蛋";
flags1=0;
break;
}
}
if(flags1)
{// cout<<"我是大笨蛋";
strncpy(p,s[0]+k,j);
// p[j]='\0';
return p;
}
}
}
//
void main()
{
const char *p1[]={"fsdfsdf","gsgssfsd","ryrghgjgfsd"};
char *q=(char *)malloc(sizeof(char)*20);
memset(q,0,sizeof(q));
cout<<maxchar(p1,q,3);
}
不需要调用子函数的
#include <iostream>
using namespace std;
void main()
{
const int n=3;
char *s[]={"fsdfsdf","gsgssfsd","ryrghgjgfsd"};
char *p=(char *)malloc(sizeof(char)*20);
memset(p,0,sizeof(p));
int j=0,k=0,l=0,m=0;
for(j=strlen(s[0]);j>0;j--)//最大字符串的长度
for(k=0;k+j-1<strlen(s[0]);k++)//最大字符串开始的位置
{int flags1=1;
for(l=1;l<n;l++)
{int flags2=0;
for(m=0;m+j-1<strlen(s[l]);m++)
{
if(strncmp(s[0]+k,s[l]+m,j)==0)
{cout<<"我是大笨蛋";
flags2=1;
break;
}
}
if(!flags2)//如果循环到这个地方有某个字符串中没有s[0]中挑出的那个字符串,说明这个字符串不是最大的,将代表最大字符串的标志位设为
{//cout<<"我是大笨蛋";
flags1=0;
break;
}
}
if(flags1)
{// cout<<"我是大笨蛋";
strncpy(p,s[0]+k,j);
goto L;
}
}
L:cout<<p;
}
34.超大整数加法运算
大整数会用字符串或者数组来存,不过注意低位存字符前面几位,高位存后面,存到字符中应该存“”。这边我用的是数组
#include <string>
#include<iostream>
using namespace std;
#define ln 100 //数字长度
void bigint(char a[],char b[])
{
int la = 0, lb = 0;
int A[ln]={0};
int B[ln]={0};
la =strlen(a);
for (int i=0; i<la; i++)
{
A[i] = int(a[la-1-i])-48;
}
lb =strlen(b);
for (int i=0; i<lb; i++)
{
B[i] = int(b[lb-1-i])-48;
}
int n=(la>lb)?la:lb;
for (int i=0; i<n; i++)
{
A[i]=A[i]+B[i];
A[i+1]=A[i+1]+A[i]/10;//将进位加到A对应位shang A[i]=A[i]%10;//进位后原位只留下个位
}
cout << "相加结果:" << endl;
for (int j=n; j>=0; j--)
{
cout << A[j];
}
cout << endl;
}
void main()
{
char a[ln];
cout << "输入一个高精度数(小于位)作被加数:" << endl; cin.getline(a, ln);
char b[ln];
cout << "输入另一个高精度数(小于位)作加数:" << endl; cin.getline(b, ln);
bigint(a,b);
}
35.排序总结
//交换排序1:冒泡法
#include<iostream>
using namespace std;
void BubbleSort(int a[],int length)
{ int temp=0;
for(int i=0;i<length-1;i++)
for(int j=0;j<length-1-i;j++)
{
if(a[j]>a[j+1])
{temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
//交换排序2: 鸡尾酒排序: 鸡尾酒排序,又被称作双向冒泡排序,是一种从冒泡算法演变成的稳定排序算法,不同于冒泡算法重复的从头到尾比较,鸡尾酒算法交替的从头到尾再从尾到头比较关键字。该算法的性能比标准的冒泡算法稍高。
注意:鸡尾酒算法可以看作是选择算法的一种变体。
void CocktailSort(int *array, int length)
{
int i;
int temp;
int left = 0, right = length;
int finished;
do
{
finished = 1;
--right;
for (i = left; i < right; i++)
{
if (array[i] > array[i+1])
{
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
finished = 0;
}
}
if (finished)
{
return;
}
finished = 1;
for (i = right - 1; i > left; i--)
{
if (array[i] < array[i-1])
{
temp = array[i];
array[i] = array[i-1];
array[i-1] = temp;
finished = 0;
}
}
++left;
} while (!finished);
}
//交换排序3:快速排序: 从数列中挑出一个元素,称为"基准"(pivot)。
重新排叙述列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任ㄧ边)。在这个分割之后,该基准是它的最后位置。这个称为分割(partition)操作。
递回地(recursive)把小于之元素的子数列和大于之元素的子数列排序。
递回的最底部情形,是数列的大小是零或一,也就使永远都已经被排序好了。虽然一直递回下去,但是这个算法总会结束,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。
int Partition(int *array, int low, int high)
{
int pivot_val = array[low];
int temp;
while (low < high)
{
while (low < high && array[high] >= pivot_val)
{
--high;
}
temp = array[low];
array[low] = array[high];
array[high] = temp;
while (low < high && array[low] <= pivot_val)
{
++low;
}
temp = array[high];
array[high] = array[low];
array[low] = temp;
}
return low;
}
void _QuickSort(int *array, int low, int high)
{
int pivot_loc;
if (low < high)
{
pivot_loc = Partition(array, low, high);
_QuickSort(array, low, pivot_loc - 1);
_QuickSort(array, pivot_loc + 1, high);
}
}
void QuickSort(int *array, int length)
{
_QuickSort(array, 0, length - 1);
}
//选择排序:直接选择排序:首先在未排序序列中找到最小元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小元素,然后放到排序序列末尾。以此类推,直到所有元素均排序完毕。 void SimpleSelectionSort(int a[],int length)
{
int minnum=0,i=0,j=0,temp=0;
for(i=0;i<length-1;i++)
{minnum=i;
for(j=i;j<length;j++)
{
if(a[j]<a[minnum])
minnum=j;
}
temp=a[i];
a[i]=a[minnum];
a[minnum]=temp;
}
}
//插入排序1:简单插入排序: 它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。
插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。
void StraightInsertionSort(int a[],int length)
{
int temp=0,i=0,j=0;
for(i=1;i<length;i++)
{
temp = a[i];//把i之前的大于a[i]的元素往后移
for(j = i - 1; j >= 0 && temp < a[j]; j--)//这边之所以从i - 1以后--是避免数据被覆盖丢失 {
a[j+1] = a[j];
}
a[j+1] = temp;//在合适位置安放a[i]
}
}
//插入排序2: 二分法查找插入排序
如果比较操作的代价比交换操作大的话,可以采用二分查找法来减少比较操作的树目。该算法可以认为是插入排序的一个变种,称为二分查找排序。折半插入排序所需附加存储空间和直接插入排序相同,从时间上比较,折半插入排序仅减少了关键字间的比较次数,而记录的移动次数不变。
其实就是二分法查找与插入排序的一个结合,在已排好的字符串中用二分法查找出那个最合适的插入位置(找到的一般是比a[i]小的,即将离其最近的一个下标n),插入位置就是n+1
//void BinaryInsertionSort(int *array, int length)
//{
// int i, j;
// int temp;
// int low, high, mid;
// for (i = 1; i < length; i++)
// {
// temp = array[i];
// low = 0;
// high = i - 1;
// while (low <= high)
// {
// mid = (low + high) / 2;
// if (temp < array[mid])
// {
// high = mid - 1;
// }
// else
// {
// low = mid + 1;
// }
// }
// for (j = i - 1; j >= high + 1; j--)
// {
// array[j+1] = array[j];
// }
// array[high+1] = temp;
// }
//}
//插入排序3:希尔排序:先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行直接插人排序;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量dt=1(dt<dt-1<…<d2<d1),即所有记录放在同一组中进行直接插入排序为止。
void ShellInsert(int *array, int length, int dk)
{
int i, j;
int temp;
for (i = dk; i < length; i++)
{
temp = array[i];
for (j = i - dk; j >= 0 && temp < array[j]; j -= dk)
{
array[j+dk] = array[j];
}
array[j+dk] = temp;
}
}
void ShellSort(int *array, int length, int *gap, int count)
{
int i;
for (i = count - 1; i >= 0; i--)
{
ShellInsert(array, length, gap[i]);
}
}
void shellSort(int a[],int length)
{
int gap[] = {1,2,3,5,8,13,21,34,55,89};
ShellSort(a,length,gap,9);
}
合并排序:归并操作(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作。归并操作的工作原理如下:
1.申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列。
2.设定两个指针,最初位置为别为两个已经排序序列的起始位置。
3.比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置。
4.重复步骤直到某一指针达到序列尾。
5.将另一序列剩下的所有元素直接复制到合并序列尾。
归并排序具体工作原理如下(假设序列共有n个元素):
1.将序列每相邻两个数字进行归并操作(merge),形成floor(n / 2)个序列,排序后每个序列包含两个元素。
2.将上述序列再次归并,形成floor(n / 4)个序列,每个序列包含四个元素。
3.重复步骤,直到所有元素排序完毕。
//void Merge(int array[], int first, int mid, int last)
//{
// int i, j = 0;
// int begin1 = first, end1 = mid, begin2 = mid + 1, end2 = last;
// int *temp = (int *)malloc((last - first + 1) * sizeof(int));
// if (!temp)
// {
// fprintf(stderr, "\n内存分配失败,程序将强制退出!\n");
// getchar();
// exit(1);
// }
// while (begin1 <= end1 && begin2 <= end2)
// {
// if (array[begin1] < array[begin2])
// {
// temp[j++] = array[begin1]; begin1++;
// }
// else
// {
// temp[j++] = array[begin2]; begin2++;
// }
// }
// while (begin1 <= end1)
// {
// temp[j++] = array[begin1++];
// }
// while(begin2 <= end2)
// {
// temp[j++] = array[begin2++]; // }
// for (i = 0; i < (last - first + 1); i++) // {
// array[first + i] = temp[i]; // }
// free(temp);
//}
//
//void _MergeSort(int *array, int first, int last) //{
// int mid;
// if (first < last)
// {
// mid = (first + last) / 2;
// _MergeSort(array, first, mid); // _MergeSort(array, mid + 1, last); // Merge(array, first, mid, last); // }
//}
//
//void MergeSort(int *array, int length) //{
// _MergeSort(array, 0, length - 1); //}
void main()
{
int a[7]={5,6,3,7,4,2,1};
// BubbleSort(a,5);
// SimpleSelectionSort(a,5);
// StraightInsertionSort(a,5);
// QuickSort(a,5);
shellSort(a,7);
for(int i=0;i<7;i++)
cout<<a[i];
}
36.将一个字符串空格分隔,并倒序输出 #include<iostream>
#include<string>
using namespace std;
char * strstr1(char *ms)
{
int n1=strlen(ms);
char *s1=(char *)malloc(sizeof(char)*(n1+3));
memset(s1, 0, n1+3 );//必须初始化,否则会有乱码
char *s[100];
for(int n3=0;n3<100;n3++)
{
s[n3]=(char *)malloc(20*sizeof(char));
memset(s[n3], 0, 20 );//可以不初始化,为什么? }
const char * split =" ";
char *p={0};
int i=0;
p = strtok (ms,split);
while(p!=NULL) {
strcpy(s[i],p);
i++;
p = strtok(NULL,split);
}
for(int j=0;j<i;j++)
{strcat(s1,s[i-1-j]);
strcat(s1," ");
}
return s1;
}
void main()
{
char a[]="i am stupid.";
cout<<strstr1(a);
}
此函数用时经常会出现错误指针的问题,尤其是程序中的p指针,虽然是错指针,但是不影响
后面的结果。
字符串数组使用前一定要给每一个字符串分配内存。否则会出现“写入位置 0x01037871 时发生访问冲突”。
后面的strcat(s1,s[i-1-j]);用之前先要对s1初始化,否则会出现乱码。
37删除一个字符串中的某个字符串
#include<vector>
#include<iostream>
#include<string>
using namespace std;
char * fun(char * s1, char* a1)
{
string s(s1);
while(s.find(a1)!=string::npos)
{
s.erase(s.find(a1),strlen(a1));
}
char * m=new char[20];
strcpy(m,s.c_str());//注意这边的string转成char*返回值是const char*,不能直接将其return,而且不能直接赋给char*,需要用strcpy来做。
cout<<m;
return m;
}
void main()
{
char *s1="fdsffdg";
char* a1("fd");
cout<<fun(s1,a1);
}
38.取出一个字符串中所有的数字,并取出所有字母
#include<vector>
#include<iostream>
#include<string>
using namespace std;
char * fun(char * s1, char* a1)
{
string s(s1);
string s2;
string s3;
string::size_type pos=0;
while((pos=s.find_first_of(a1,pos))!=string::npos)
{
s2+=s[pos];
pos++;
}
pos=0;
while((pos=s.find_first_not_of(a1,pos))!=string::npos)
{
s3+=s[pos];
pos++;
}
char * m=new char[20];
char * m1=new char[20];
strcpy(m,s2.c_str());
strcpy(m1,s3.c_str());
//cout<<s;
cout<<m;
cout<<m1;
return m;
}
void main()
{
char *s1="fs2dsf3f3dg5";
char* a1("0123456789");
cout<<fun(s1,a1);
}
39.简单的一个实现字符统计的 #include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
void main()
{int a[128];
memset(a,0,sizeof(a));
string s="gdssdsgs";
int n=s.size();
int i = 0;
for ( i = 0; i<n; i++ )
{ int p=s[i];
a[p]+=1;}
for (int j = 0;j<128; j++ )
{if(a[j]>0 )
cout<<(char)j<<":"<<a[j];
}
}
#include <stdio.h>
#include <string.h>
#define MAX_N 20 //字符串的最大数目
#define MAX_LEN 256 //字符串的最大长度(含结束符'\0') int N;//输入的字符串数
char str[MAX_N][MAX_LEN];//保存所有字符串
int Len[MAX_N];//保存字符串的长度
//字符串匹配,成功返回,失败返回
int str_match(char *s1, char *s2, int len)
{
while(len > 0)
{
if(*s1 != *s2)
return 0;
s1++; s2++; len --; } return 1; } //main void main() { int i,s,t,l; printf("请输入字符串的数目N(N<=20):"); scanf("%d",&N); gets(str[0]); printf("请输入%d个字符串(每个字符串以回车结束):\n",N); for(i=0; i<N; i++) { gets(str[i]); Len[i] = strlen(str[i]); } for(l=Len[0]; l>0; l--) { for(s=0; s+l-1 < Len[0]; s++) { int flag1 = 1; for(i=1; i<N; i++) { int flag2 = 0; for(t=0; t+l-1 < Len[i]; t++) { if(str_match(str[0]+s,str[i]+t,l)) { flag2 = 1; break; } } //失败 if(!flag2) { flag1 = 0; break; } } //成功 if(flag1) { goto L;//跳到输出 } } } //输出最长公共子串 printf("最长公共子串为:"); L: for(i=0; i<l; i++)
{ printf("%c",*(str[0]+s+i)); } printf("\n"); }
40.查找字符串中空格分隔的单词的最大长度 //
//查找字符串中空格分隔的单词的最大长度
//
#include<iostream>
using namespace std;
void charmain(const char * p,char *s)
{ int i=0,j=0,k=0,l=0,max=0,m=0;
int len=strlen(p);
while(p[i]!='\0')
{
while(p[i]!=' ' && i<len)
{
j++;
i++;
}
if(j>max)
{
max=j;
k=i;
}
j=0;
i++;
}
//for(l=0;i<max)
for(l=k-max;l<k;l++)
{ s[m]=p[l];
m++;
}
s[m]='\0';
cout<<s;
//free(p);
}
void main()
{
const char *a="dsf fd fsdf";
char *b=(char *)malloc(sizeof(char)*strlen(a)); memset(b,0,sizeof(b));
charmain(a,b);
}
41.二叉树的操作
//二叉树,不同的输入方式有不同的构造方式 #include<iostream>
using namespace std;
typedef struct node
{
char ch;
struct node *lchild;
struct node *rchild;
}node;
//node * creat(node *bt,int k)
//{
// char a;
// node *p,*t=(node *)malloc(sizeof(node)); // //cout<<"输入数据"<<endl;
// cin>>a;
// if(a!='0')
// { p=(node *)malloc(sizeof(node));
// p->ch=a;p->lchild=NULL;p->rchild=NULL; // if(k==0){t=p;}
// if(k==1){bt->lchild=p;}
// if(k==2){bt->rchild=p;}
// creat(p,1);
// creat(p,2);
// }
// return t;
//
//}
node* creat(node* T)
{
char a;
scanf("%c",&a);
if(a=='#')
T=NULL;
else
{
if(!(T=(node*)malloc(sizeof(node)))) printf("Error!");
T->ch=a;
T->lchild=creat(T->lchild); T->rchild=creat(T->rchild); }
return T;
}
void pretrav(node * bt)
{
if(bt!=NULL)
{
cout<<bt->ch<<" ";
pretrav(bt->lchild);
pretrav(bt->rchild);
}
return;
}
void midtrav(node * bt)
{
if(bt!=NULL)
{
midtrav(bt->lchild);
cout<<bt->ch<<" ";
midtrav(bt->rchild);
}
return;
}
void postrav(node * bt)
{
if(bt!=NULL)
{
postrav(bt->lchild);
postrav(bt->rchild);
cout<<bt->ch<<" ";
}
return;
}
//int nodeTotal(node *bt)
//{
// if(bt==NULL)return 0;
// else
// {
// return 1+nodeTotal(bt->lchild)+nodeTotal(bt->rchild); // }##ode *bt)
//{
// if(bt==NULL)return 0;
// else
// {
// return 1+nodeTotal(bt->lchild)+nodeTotal(bt->rchild); // }
//}
void main()
{
node *bt=(node *)malloc(sizeof(node));
node *bt1=(node *)malloc(sizeof(node)); bt->lchild=NULL;
bt->rchild=NULL;
bt1=creat(bt);
pretrav(bt1);
cout<<endl;
midtrav(bt1);
cout<<endl;
postrav(bt1);
cout<<endl;
}
42.分块查找
#include<iostream>
using namespace std;
struct indexnode
{
int max;
int index;
};
int search(struct indexnode s[],int m,int v[],int n,int x)
{ int i=0,j=m,t=0;
while (j-i>1)
{
t=(j+i)/2;
if(x<=s[t].max)
j=t;
else
{
i=t;
}
}
if((j-i==1)&&x>s[i].max)
{
i=j;
}
j=s[i].index;
if(i!=m)
{ t=s[i+1].index;
}
else{t=m;}
int k=0;
for( k=j;k<t;k++)
{
if(v[k]==x)
{ j=k;
break;
}
}
if(k==t)
j=-1;
return j;
}
void main()
{
struct indexnode s[3]={{22,0},{46,6},{86,12}};
int a[]={22,12,13,8,9,20,33,42,44,38,24,46,60,58,74,47,86,53}; cout<<search(s,3,a,18,38);
}
43.赫夫曼树
// 赫夫曼树和赫夫曼编码的存储表示
//#include <stdio.h>
//#include <limits.h>
//#include <malloc.h>
//#include <string.h>
//#include <stdlib.h>
//
//typedef struct
//{
// unsigned int weight;
// unsigned int parent,lchild,rchild;
//}HTNode,*HuffmanTree; // 动态分配数组存储赫夫曼树 //
//typedef char **HuffmanCode; // 动态分配数组存储赫夫曼编码表 //
//// 函数void select()调用
//int min1(HuffmanTree t,int i)
//{
// int j,flag;
// unsigned int k=UINT_MAX; // 取k为不小于可能的值 // for(j = 1; j <= i; j++)
// if(t[j].weight < k && t[j].parent == 0)
// k = t[j].weight,flag = j;
// t[flag].parent=1;
// return flag;
//}
//
//// s1为最小的两个值中序号小的那个
//void select(HuffmanTree t,int i,int *s1,int *s2)
//{
// int j;
// *s1=min1(t,i);
// *s2=min1(t,i);
// if(*s1 > *s2)
// {
// j=*s1;
// *s1=*s2;
// *s2=j;
// }
//}
//
//// 算法.12 P147
//// w存放n个字符的权值(均>0),构造赫夫曼树HT,并求出n个字符的赫夫曼编码HC //void HuffmanCoding(HuffmanTree *HT,HuffmanCode *HC,int *w,int n)
//{
// int m,i,s1,s2,start;
// unsigned c,f;
// HuffmanTree p;
// char *cd;
//
// if(n<=1)
// return;
// m=2*n-1; //因为一颗有n个叶子结点的赫夫曼树共有n-1个结点
// *HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode)); // 0号单元未用
// for(p=*HT+1,i=1;i<=n;++i,++p,++w) //初始化各结点的权值
// {
// (*p).weight=*w;
// (*p).parent=0;
// (*p).lchild=0;
// (*p).rchild=0;
// }
// for(;i<=m;++i,++p) //初始化双亲位置
// (*p).parent=0;
// for(i=n+1;i<=m;++i) // 建赫夫曼树
// {
// // 在HT[1 ~ i-1]中选择parent为且weight最小的两个结点,其序
// // 号分别为s1和s2
// select(*HT,i-1,&s1,&s2);
// (*HT)[s1].parent=(*HT)[s2].parent=i;
// (*HT)[i].lchild=s1;
// (*HT)[i].rchild=s2;
// (*HT)[i].weight=(*HT)[s1].weight+(*HT)[s2].weight;
// }
// // 从叶子到根逆向求每个字符的赫夫曼编码
// *HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
// // 分配n个字符编码的头指针向量([0]不用)
// cd=(char*)malloc(n*sizeof(char)); // 分配求编码的工作空间
// cd[n-1]='\0'; // 编码结束符
// for(i=1;i<=n;i++)
// {
// // 逐个字符求赫夫曼编码
// start=n-1; // 编码结束符位置
// for(c=i,f=(*HT)[i].parent; f!=0; c=f,f=(*HT)[f].parent)
// // 从叶子到根逆向求编码
// if((*HT)[f].lchild==c)
// cd[--start]='0';
// else
// cd[--start]='1';
// (*HC)[i]=(char*)malloc((n-start)*sizeof(char));
// // 为第i个字符编码分配空间
// strcpy((*HC)[i],&cd[start]); // 从cd复制编码(串)到HC // }
// free(cd); // 释放工作空间
//}
//
//int main()
//{
// HuffmanTree HT;
// HuffmanCode HC;
// int *w,n,i;
//
// printf("请输入权值的个数(>1):");
// scanf("%d",&n);
// w=(int*)malloc(n*sizeof(int));
// printf("请依次输入%d个权值(整型):\n",n);
// for(i=0;i<=n-1;i++)
// scanf("%d",w+i);
// HuffmanCoding(&HT,&HC,w,n);
// for(i=1;i<=n;i++)
// puts(HC[i]);
//
// system("pause");
// return 0;
//}
/*
输出效果:
请输入权值的个数(>1):
请依次输入个权值(整型):
7 5 2 4
10
110
111
请按任意键继续. . .
*/
43.我写的赫夫曼树
#include<iostream>
using namespace std;
typedef struct node
{
int w;
int lc,rc,par;
}holftree;
typedef char ** holf;
void select(holftree * p1,int l,int *s1,int *s2)
{
int v=0;
int min=100,min2=100,flag=0,flag2=0,z=0;
for(v=0;v<l;v++)
{if(p1[v].w<min && p1[v].par==0)
{
min=p1[v].w;
flag=v;
}
}
*s1=flag;
//cout<<"s1:"<<*s1<<endl;
for(z=0;z<l;z++)
{
if(p1[z].w<min2 && p1[z].par==0 && z!=flag)
{
min2=p1[z].w;
flag2=z;
}
}
*s2=flag2;
//cout<<"s2:"<<*s2<<endl;
}
holftree *holfcreat(int n ,int w[])
{ int s1,s2;
int m=2*n-1;//一个赫夫曼树有n个叶节点,那么就有n-1个节点 holftree *s=(holftree *)malloc(sizeof(holftree)*m);
for(int i=0;i<n;i++)
{
s[i].w=w[i];
s[i].lc=0;
s[i].rc=0;
s[i].par=0;
}
for(int j=n;j<m;j++)
s[j].par=0;
for(int k=n;k<m;k++)
{
select(s,k,&s1,&s2);
s[s1].par=s[s2].par=k;
s[k].lc=s1;
s[k].rc=s2;
s[k].w=s[s1].w+s[s2].w;
}
return s;
}
void holfcode(holftree * p2,int n1,int n3,char *holfmancode[]) {
char *holfman=(char *)malloc(sizeof(char)*n3);
memset(holfman,0,sizeof(holfman));
int r=0;
holfman[n3-1]='\0';
int c=0;
int t;
for(r=0;r<n3;r++)
{ int start=n3-1;
for(c=r,t=p2[r].par;t!=0;c=t,t=p2[t].par)
{if(p2[t].lc==c)
{
holfman[--start]='0';
}
if(p2[t].rc==c)
{
holfman[--start]='1';
}
}
holfmancode[r]=(char *)malloc(sizeof(char)*(n3-start)); //memset(holfmancode[r],0,sizeof(holfmancode[r])); strcpy(holfmancode[r],holfman+start);
//cout<<holfmancode[r];
}
}
void main()
{
cout<<"请输入个数n:";
int n;
cin>>n;
cout<<"请输入权重数组w[n]:";
int *w=(int *)malloc(sizeof(int)*n);
for(int j=0;j<n;j++)
cin>>w[j];
cout<<endl;
holftree *p=(holftree *)malloc(sizeof(holftree)*(2*n-1));
char * *holfmancode=(char **)malloc(sizeof(char *)*n); p=holfcreat(n,w);
//for(int u=0;u<2*n-1;u++)
//cout<<p[u].par;
holfcode(p,2*n-1,n,holfmancode);
for(int o=0;o<n;o++)
cout<<holfmancode[o]<<endl;
}
44.二叉排序树
#include<iostream>
using namespace std;
typedef struct node
{
int data;
struct node *lc;
struct node *rc;
}node;
node *treecreat(int b[],int n)
{
node *p,*q,*bt;
bt=NULL;//设好初始值
for(int i=0;i<n;i++)
{
p=(node *)malloc(sizeof(node));
p->data=b[i];
p->lc=NULL;
p->rc=NULL;
if(bt==NULL)
bt=p;
else
{q=bt;//一定要写在这边,这样q在起始都是从根树开始找
while((q->lc!=p)&&(q->rc!=p))//如果没有合适的数,继续循环
{
if(b[i]<q->data)//比根树小的放在左子树,看左子树有无树枝,有就往下循环,再按照上面的;流程,大的放右边,小的放左边
{
if(q->lc==NULL)
{q->lc=p;}
else
{ q=q->lc;}
}
else
{
if(q->rc==NULL)
{ q->rc=p;}
else
{q=q->rc;}
}
}
}
}
return bt;
}
void midtrav(node *m)//中序遍历就是排序 {
if(m!=NULL)
{
midtrav(m->lc);
cout<<m->data<<" ";
midtrav(m->rc);
}
return;
}
node *research(node *s,int x)
{
node *p=(node *)malloc(sizeof(node)); p=s;
while(p!=NULL && p->data!=x) //比较与根节点的大小,大的在右子树,小的在左子树 {
if(p->data>x)
p=p->lc;
if(p->data<x)
p=p->rc;
}
return p;//未找到就返回跳出循环时的p=NULL。 }
void main()
{
int a[]={4,6,2,7,1,3,5};
node *t=(node *)malloc(sizeof(node)); t=treecreat(a,7);
midtrav(t);
cout<<"输入要查找的数据"<<endl;
int b;
cin>>b;
cout<<research(t,b)->data<<endl;
}
45.堆排序
基本思想是:先将完全二叉树以数组方式储存,从根节点—左子树-右子树的顺序,那么n/2个元素就是最后一个非叶子的节点。将这个元素与其左右子树中较大的a那个比较,比其小就交换。然后交换后再将这个元素与a的子树中大的比较.循环结束后。进行第二步,将第一个元素与最后一个元素交换,然后除去最后一个元素剩下的元素重新建堆,循环往复下去直到堆为空 #include<iostream>
using namespace std;
void heapcreat(int *p,int m,int n)
{
int t=p[m];
int j=2*m+1;//这边之所以加一是因为是从开始的
while(j<=n)
{ if((j<n)&&(p[j]<p[j+1]))//找出左右子树较大的那个
j=j+1;
if(t<p[j])//交换
{ p[m]=p[j];
m=j;//记录t应该在的位置
j=2*m+1;//移到下一个左子树,继续循环进行比较
}
else j=n+1;
}
p[m]=t;
}
int *heapsort(int b[],int n)
{
int k=n/2;
for(int j=k-1;j>=0;j--)//从最后一个非叶子节点开始
heapcreat(b,j,n-1);//建堆
for(int l=n-1;l>=1;l--)//第一个元素与最后一个元素交换,然后对剩下的元素重新建堆 { b[0]=b[0]^b[l];
b[l]=b[0]^b[l];
b[0]=b[0]^b[l];
heapcreat(b,0,l-1);//第一个元素换掉了,因此从第一个元素开始建堆
}//建堆
return b;
}
void main()
{
int a[]={4,6,2,7,1,3,5};
int *t=(int *)malloc(sizeof(int)*7); t=heapsort(a,7);
for(int i=0;i<7;i++)
cout<<t[i];
}