C++ 指標練習題

來源:互聯網
上載者:User

 本文轉載自: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++實現,禮拜四提交給班長

 

為方便以後查閱,才轉載至此,並無侵權之意。謝謝原作者。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.