现在的位置: 首页 > 综合 > 正文

C++ 指针练习题

2013年09月03日 ⁄ 综合 ⁄ 共 3615字 ⁄ 字号 评论关闭

 本文转载自:http://blog.chinaunix.net/uid-24219701-id-1993931.html
为方便以后查阅,才转载至此,并无侵权之意。谢谢原作者。

1.耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3……,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛徒,请找出它原来的序号

 

/*

 * 使用循环链表实现要求

 * Lzy 2011-8-1

 */

 

#include <iostream>

#include <istream>

using namespace std;

 

typedefstruct Node

{

    int data;

    struct Node *next;

}node;

 

int main(void)

{

    node *head = new node;       //创建头节点

    head->next = head;           //循环链表

    node *p = head;  

    p->data =1;              //给第一个节点赋值

 

    int i;

    for(i=12; i>0; i--)

    {

        node *p = new node;      //分配新节点

        p->data = i+1;            //从13减一开始赋值

        p->next = head->next;    //指向域指向下一个节点

        head->next = p;           //插到头节点后

    }

   

    int count=0;             

    node *q,*pq=NULL;              

    p = head;                 

 

    while(1)

    {

        count++;           //计数加一

        q = p;            //保存前一节点方便删除

        p=p->next;        //沿链表移动指针

       

        if(count==2)      //计数到3

        {

            if(pq == p)       //链表只剩最后一个节点

            {

                cout<<p->data<<endl;

                return0;

            }

            /*删除节点*/

            q->next = p->next;

            delete[] p;

           

            count=0;   //计数清0

            pq = p = q->next;//p指向下一节点,pq标志

        }

    }

 

    return0;

}

 

2.定义一个结构体变量(包括年、月、日),计算该日在本年中为第几天?(注意考虑闰年问题)

/* 一年的第几天

 * Lzy 2011-8-1

 */

#include <iostream>

#include <istream>

 

using namespace std;

 

struct Date

{

    int year;

    int month;

    int date;

}dat;

 

int IsLeapYear(int year)

{

    return(year%4==0&& year%100!=0)||(year%400==0);

}

 

int main(void)

{

    cout<<"输入年 月 日"<<endl;

    cin>>dat.year>>dat.month>>dat.date;

   

    int date = dat.date;

 

    switch(dat.month-1)

    {

    case11:

        date +=30;

    case10:

        date +=31;

    case9:

        date +=30;

    case8:

        date +=31;

    case7:

        date +=31;  

    case6:

        date +=30;

    case5:

        date +=31;

    case4:           

        date +=30;

    case3:       

        date +=31;

    case2:

        date += IsLeapYear(dat.year)+28;

    case1:

        date +=31;

    }

   

    cout<<"第"<<date<<"天"<<endl;

    cout<<"星期"<<(date-4)%8<<endl;

    return0;

}

 

3.给定一个日期,求出该日为星期几(已知2002-3-28为星期四)

 

4.建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中的结点包括该学号,则输出该结点内容后,并将其结点删去。

/*

 * 使用循环链表实现要求

 * Lzy 2011-8-1

 */

 

#include <iostream>

#include <istream>

#include <string.h>

using namespace std;

 

typedefstruct Node

{

    int id;

    char ***[3];

    int age;

    struct Node *next;

}node;

 

void LinkListInit(node **p)

{

    (*p)=new node;

    (*p)->next =NULL;

}

 

void LinkListInput(node *head, node *p)

{

    p->next = head->next;

    head->next = p;

}

 

void LinkListAdd(node *head)

{

    char ch;

 

    while(1)

    {

        node *p = new node;     

 

        cout<<"输入\n学号\t"<<"性别\t"<<"年龄\n";

        cin>>p->id>>p->***>>p->age;

        cout<<"Save(y\\n): ";

 

        cin>>ch;

        if(ch =='y')

            LinkListInput(head, p);

        else

            delete p;

        cout<<"Continue(y\\n): ";

 

        cin>>ch;

        if(ch =='n')

            break;

    }  

}

 

void LinkListDelete(node *head,int id)

{

    node *p = head;

    node *q=p;

 

    while(p)

    {

        q = p;

        p = p->next;

 

        if(p->id == id)

        {

            q->next = p->next;

            cout<<p->id<<'\t'<<p->***<<'\t'<<p->age<<endl;

            delete[] p;

            p = q->next;

        }      

    }

}

 

void LinkListDisplay(node *head)

{

    node *p = head->next;

 

    cout<<"\n学号\t"<<"性别\t"<<"年龄\n";

    while(p)

    {

        cout<<p->id<<'\t'<<p->***<<'\t'<<p->age<<endl;

        p = p->next;

    }  

}

 

int main(void)

{

    node *head = new node;       //创建头节点

   

    LinkListInit(&head);

    LinkListAdd(head);

 

    int id;

    cout<<"要删除的学号:";

    cin>>id;

 

    LinkListDelete(head,id);

    LinkListDisplay(head);

    return0;

}

 

 

5.有一个unsigned long型整数,先要分别将其前2个字节和后2个字节用为两个unsigned int型整数输出(设一个int型数据占2个字节),试编写一函数partition实现上述要求。要求在主函数输入该long型整数,在函数partition中输出结果

 

要求用在linux 下用c++实现,礼拜四提交给班长

 

为方便以后查阅,才转载至此,并无侵权之意。谢谢原作者。
【上篇】
【下篇】

抱歉!评论已关闭.