C ++ pointer exercises

Source: Internet
Author: User

This article Reprinted from: http://blog.chinaunix.net/uid-24219701-id-1993931.html
This record is reprinted for future convenience and has no intention of infringement. Thank you, original author.

1. jesus had 13 disciples, and one of them was a traitor who sold Jesus. Please use division to find the traitor: 13 people sat around and started from the first to report: 1, 2, 3, 1, 2, 3 ......, Anyone who reports to "3" will leave the circle. The last person in the circle is the traitor who sold Jesus. Please find out its original serial number.

 

/*

* Implementation requirements using cyclic linked lists

* 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; // create a header Node

Head-> next = head; // circular linked list

Node * P = head;

P-> DATA = 1; // assign a value to the first node

 

Int I;

For (I = 12; I> 0; I --)

{

Node * P = new node; // allocate a new node

P-> DATA = I + 1; // values are assigned starting from 13 minus 1.

P-> next = head-> next; // point to the domain to the next node.

Head-> next = P; // insert it to the header Node

}

Int COUNT = 0;

Node * q, * PQ = NULL;

P = head;

 

While (1)

{

Count ++; // count 1

Q = P; // Save the previous node for easy Deletion

P = p-> next; // move the pointer along the linked list

If (COUNT = 2) // count to 3

{

If (PQ = P) // The linked list contains only the last node.

{

Cout <p-> data <Endl;

Return0;

}

/* Delete a node */

Q-> next = p-> next;

Delete [] P;

Count = 0; // The count is 0.

PQ = P = Q-> next; // P points to the next node.

}

}

 

Return0;

}

 

2. Define a struct variable (including year, month, and day), and calculate the day of the current year as the day? (Pay attention to the leap year issue)

/* Day of the year

* 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 & amp; Year % 100! = 0) | (Year % 400 = 0 );

}

 

Int main (void)

{

Cout <"Enter year month Day" <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 <"day" <date <"day" <Endl;

Cout <"Week" <(date-4) % 8 <Endl;

Return0;

}

 

3. specify a date and determine the day of the week (Thursday is known as-3-28)

 

4. create a linked list. Each node includes the student ID, name, gender, and age. Enter a student ID. If the node in the linked list includes the student ID, output the content of the node, delete the node.

/*

* Implementation requirements using cyclic linked lists

* 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 <"enter \ n student ID \ t" <"Gender \ t" <"Age \ 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 student ID \ t" <"Gender \ t" <"Age \ n ";

While (P)

{

Cout <p-> id <'\ t' <p-> *** <' \ t' <p-> age <Endl;

P = p-> next;

}

}

 

Int main (void)

{

Node * head = new node; // create a header Node

Linklistinit (& head );

Linklistadd (head );

 

Int ID;

Cout <"student ID to be deleted :";

Cin> ID;

 

Linklistdelete (Head, ID );

Linklistdisplay (head );

Return0;

}

 

 

5. there is an unsigned long integer, first, use the first two bytes and the last two bytes as two unsigned int-type integers (set one int-type data to 2 bytes ), write a function partition to meet the above requirements. You must input this long integer in the main function and output the result in the partition function.

 

It must be implemented in C ++ in Linux and submitted to the shift leader on Thursday.

 

This record is reprinted for future convenience and has no intention of infringement. Thank you, original author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.