欢迎来到天天文库
浏览记录
ID:1771584
大小:32.34 KB
页数:12页
时间:2017-11-13
《自己整理的c面试常考程序题》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、1./*字符串倒置*/#include#includevoidfunc(char*str){intbeg,end;chartem;for(beg=0,end=strlen(str)-1;beg2、2./*约瑟夫问题*/#include#includestructNode{intdata;structNode*next;};intmain(){intm,n,i;printf("inputnumber:");scanf("%d",&m);structNode*head,*r,*p,*q;head=(structNode*)malloc(sizeof(structNode));p=head;for(i=1;i<=m;i++){r=(structNode*)mal3、loc(sizeof(structNode));r->data=i;p->next=r;p=r;}p->next=head->next;p=p->next;printf("startis:");scanf("%d",&n);intj=1;while(jnext;j++;}ints;printf("间隔为:");scanf("%d",&s);do{for(i=1;i<=s-1;i++)p=p->next;q=p->next;printf("%d",q->data);p->next=q-4、>next;free(q);p=p->next;m--;}while(m>0);return0;}3./*String类的实现*/#include#includeusingnamespacestd;classString{public:String(constchar*data=NULL){if(data==NULL){m_data=newchar[1];*m_data=' ';}else{m_data=newchar[strlen(data)+1];strc5、py(m_data,data);}}String(constString&other){m_data=newchar[strlen(other.m_data)+1];strcpy(m_data,other.m_data);}~String(void){delete[]m_data;}String&operator=(constString&other){if(this==&other)return*this;delete[]m_data;m_data=newchar[strlen(other.m_da6、ta)+1];strcpy(m_data,other.m_data);}voidprint(void){puts(m_data);}private:char*m_data;};intmain(){Strings("liushaohui");s.print();Stringp(s);p.print();String*t=newString("haojiahuo");t->print();Stringq;q=s;q.print();return0;}4./*定义一个Person类,实现堆的功能*/#inc7、ludeusingnamespacestd;classPerson{public:Person(void):m_head(NULL){}~Person(void){Node*p;while(m_head){p=m_head->m_next;deletem_head;m_head=p;}}voidPush(constchar*name,intage){m_head=newNode(name,age,m_head);}voidPop(void){if(!m_head)return;el8、se{Node*p=m_head->m_next;printf("%s,%d",m_head->m_name,m_head->m_age);deletem_head;m_head=p;}}private:classNode{public:Node(constchar*name=NULL,intage=0,Node*next):m_name(newchar[strlen(name)+1]),m_age(age),m_next(next){strcpy(
2、2./*约瑟夫问题*/#include#includestructNode{intdata;structNode*next;};intmain(){intm,n,i;printf("inputnumber:");scanf("%d",&m);structNode*head,*r,*p,*q;head=(structNode*)malloc(sizeof(structNode));p=head;for(i=1;i<=m;i++){r=(structNode*)mal
3、loc(sizeof(structNode));r->data=i;p->next=r;p=r;}p->next=head->next;p=p->next;printf("startis:");scanf("%d",&n);intj=1;while(jnext;j++;}ints;printf("间隔为:");scanf("%d",&s);do{for(i=1;i<=s-1;i++)p=p->next;q=p->next;printf("%d",q->data);p->next=q-
4、>next;free(q);p=p->next;m--;}while(m>0);return0;}3./*String类的实现*/#include#includeusingnamespacestd;classString{public:String(constchar*data=NULL){if(data==NULL){m_data=newchar[1];*m_data=' ';}else{m_data=newchar[strlen(data)+1];strc
5、py(m_data,data);}}String(constString&other){m_data=newchar[strlen(other.m_data)+1];strcpy(m_data,other.m_data);}~String(void){delete[]m_data;}String&operator=(constString&other){if(this==&other)return*this;delete[]m_data;m_data=newchar[strlen(other.m_da
6、ta)+1];strcpy(m_data,other.m_data);}voidprint(void){puts(m_data);}private:char*m_data;};intmain(){Strings("liushaohui");s.print();Stringp(s);p.print();String*t=newString("haojiahuo");t->print();Stringq;q=s;q.print();return0;}4./*定义一个Person类,实现堆的功能*/#inc
7、ludeusingnamespacestd;classPerson{public:Person(void):m_head(NULL){}~Person(void){Node*p;while(m_head){p=m_head->m_next;deletem_head;m_head=p;}}voidPush(constchar*name,intage){m_head=newNode(name,age,m_head);}voidPop(void){if(!m_head)return;el
8、se{Node*p=m_head->m_next;printf("%s,%d",m_head->m_name,m_head->m_age);deletem_head;m_head=p;}}private:classNode{public:Node(constchar*name=NULL,intage=0,Node*next):m_name(newchar[strlen(name)+1]),m_age(age),m_next(next){strcpy(
此文档下载收益归作者所有