题目一
【问题描述】
1.定义一个学生类,包含姓名、年龄、绩点,要求能够初始化学生信息(定义常规构造函数和拷贝构造函数)、显示学生信息、设置学生信息;
2.在主函数中定义一个学生数组(普通数组或者动态数组都可以),大小为5,然后利用设置学生信息的成员函数输入每个学生信息;
3.定义student类的友元函数Sort实现按绩点排序,以对象指针(或对象数组)和数组大小为参数,对学生按绩点由高到低排序。在主函数中调用函数sort,输出排序后的学生信息。
在此基础上,为student类增加:
(1)静态数据成员 count,负责统计学生总数,并实现其值的更新;
(2)Date 类的成员对象 birthday,并为student类定义相应的构造函数;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   | #include<iostream> #include<string> using  namespace  std; class  Date {         int  year;         int  month;         int  day; public:         Date(int  year=0,int  month=0,int  day=0);         int  Getyear(){return  year;}         int  Getmonth(){return  month;}         int  Getday(){return  day;} };
 
 
  class  Student {         static  int  Count;         string  Name;         int  Age;         double  Point;         Date  Birthday; public:         Student();         Student(string  name,int  age,double  point,Date  birthday);         Student(const  Student&a);         ~Student();         int  Getcount();         void  Show();         void  Set(string  name,int  age,double  point,Date  birthday);         friend  void  Sort(Student  a[],int  b); };
 
 
  int  main() {         cout<<"请输入学生信息:"<<endl;         string  name;int  age;double  point;         int  year,month,day;         Student  a[5];         for(int  i=0;i<5;i++)         {                 cin>>name>>age>>point>>year>>month>>day;                 Date  birthday(year,month,day);                 a[i].Set(name,age,point,birthday);         }         Sort(a,5);         cout<<"共有学生"<<a[0].Getcount()<<"名。"<<endl;         cout<<"分别为:"<<endl;         for(int  i=0;i<5;i++)         {                 a[i].Show();         }         return  0; }
   | 
 
【代码】
1 2 3 4 5 6 7 8 9
   | Date::Date(int  year,int  month,int  day) { 	this->year=year; 	this->month=month; 	this->day=day; } #include<iomanip>
 
 
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
   | Student::Student() {     ; } Student::~Student() { 	; } void Student::Set(string  name,int  age,double  point,Date  birthday) { 	Name=name; 	Age=age; 	Point=point; 	Birthday=birthday; }
  int Student::Getcount() { 	return 5; } void  Sort(Student  a[],int  b) { 	for(int i=0;i<5-1;i++) 	{                 Student t; 		for(int j=i+1;j<b;j++) 		{ 			if(a[j].Point>a[i].Point) 			{ 				t=a[j]; 				a[j]=a[i]; 				a[i]=t; 			} 		} 	} } void Student::Show() { 	cout<<left<<setw(8)<<Name<<left<<setw(8)<<Age<<Birthday.Getyear()<<"."<<Birthday.Getmonth()<<"."<<left<<setw(8)<<Birthday.Getday()<<Point<<endl; }
   | 
 
题目二
【问题描述】
基于单循环链表的约瑟夫环问题。n个人围成一圈,从第一个人开始报数1、2、3,凡报到3者退出圈子,找出最后留在圈子中的人的序号。请实现下列解决约瑟夫环问题的单循环链表类Joseph,使程序正确运行。当单向链表中的尾结点指向头结点即构成单循环链表。
如果输入为0,输出 No one!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   | struct  node
  {
              int  data;
              node*  next;
              node(int  d,node*  n=NULL):data(d),next(n)  {}
  };
 
 
  class  Joseph
  {
  private:        
              node*  head;
  public:
              Joseph(int  n);
              ~Joseph();
              void  simulate();
  };
 
 
  int  main()
  {
              int  n;
              cin>>n;          
              Joseph  jos(n);
              jos.simulate();
              
              
              return  0;
  }
 
 
   | 
 
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   |  using namespace std; Joseph::Joseph(int n) {     head = NULL;     node* cur = NULL;        for (int i = 1;i <= n;i++)      {         node* temp = new node(i);         if (head == NULL) {             head = temp;             cur = temp;         }         else {             cur->next = temp;             cur = temp;         }     }     if (cur != NULL) cur->next = head; }
  void Joseph::simulate()  {     if (head == NULL)      {         cout << "No one!"; return;     }     int printOrder = 0;     int cnt = 1;     node* pre = NULL;     node* cur = head;     while (cur->next != cur) {         if (cnt == 3) {             node* temp = cur;             pre->next = cur->next;             cur = cur->next;             cnt = 1;             if (printOrder == 0) {                 cout << temp->data;                 printOrder = 1;             }             else cout << " " << temp->data;             delete temp;         }         else {             pre = cur;             cur = cur->next;             cnt++;         }     }     cout << endl;     cout << cur->data;     head = cur; }
  Joseph::~Joseph() {     if (head != NULL) delete head; }
 
 
  | 
 
代码可能略有不足,请大佬多多指教