面向对象程序设计作业 5.1~5.5

面向对象程序设计作业 5.1~5.5


面向对象编程基础两道、想法题一道、链表两道(同一道题两种做法:一道双向链表、一道单项链表)。

#include <iostream>
#include <cstdio>
using namespace std;
int daysEveryMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
class Date {
private:
    int year;
    int month;
    int day;
public:
    void setYMD(int y,int m,int d) {
        year=y;
        month=m;
        day=d;
    }
    bool isLeapYear() {
        if (year%100 == 0) {
            if (year%400 == 0) {
                return true;
            }
        }else {
            if (year%4 == 0) {
                return true;
            }
        }
        return false;
    }
    int totalDays() {
        int result=day;
        for (int i=0; i<month-1; i++) {
            result+=daysEveryMonth[i];
        }
        if (isLeapYear() && month>2) {
            result++;
        }
        return result;
    }
};
int main() {
    int y,d,m,flag=1;
    while (cin >> y >> m >> d, y!=0||d!=0||m!=0) {
        Date today;
        today.setYMD(y, m, d);
        if (flag==0) cout << endl;
        cout << today.totalDays() ;
        flag=0;
    }
    return 0;
}

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class student{
private:
    string name;
    int height;
    int weight;
public:
    void setStu(string n,int h,int w) {
        name=n;
        height=h;
        weight=w;
    }
    void replace(string n,int h,int w) {
        if (h>height) {
            setStu(n, h, w);
        }
    }
    bool notEmpty() {
        if (height!=-1) {
            return true;
        }
        return false;
    }
    void print() {
        cout << name << " " << height << " " << weight ;
    }
};
student stu[1000005];
int main() {
    for (int i=0; i<1000001; i++) {
        stu[i].setStu("0", -1, -1);
    }
    int n,num,height,weight;
    string name;
    cin >> n;
    while (n--) {
        cin >> num >> name >> height >> weight ;
        stu[num].replace(name, height, weight);
    }
    int flag=0;
    for (int i=0; i<1000000; i++) {
        if (stu[i].notEmpty()) {
            if (flag==1) cout << endl;
            flag=1;
            cout << setfill('0');
            cout << setw(6) << i << " ";
            stu[i].print();
        }
    }
    return 0;
}

#include<iostream>
using namespace std;
const int N=100005;
int a[N];
int main()
{
    int n,m,i,j;
    cin >> n >> m;
    int num=0,nmax=0;
    for (i=n; i<m; i++) {
        a[i]=0;
        for (j=1; j*j<i; j++) {
            if (i%j==0) {
                a[i]+=2;
            }
        }
        if (j*j==i) {
            a[i]++;
        }
        if (nmax<a[i]) {
            nmax=a[i];
            num=i;
        }
    }
    cout << "[" << n << "," << m << "] " << num << " " << nmax << endl;
    for (i=1; i<=num; i++) {
        if (num%i==0) {
            if (i==1) {
                cout << 1;
            }else {
                cout << " " << i;
            }
        }
    }
    return 0;
}

#include<iostream>
using namespace std;
struct Node {
    int num;
    Node *next,*last;
};
int main() {
    int k;
    cin >> k;
    while (k--) {
        Node *p1,*p2,*head;
        p1=p2=new Node;
        head=p1;
        int num;
        while (cin >> num,num!=-1) {
            p1=new Node;
            p2->next=p1;
            p1->last=p2;
            p1->num=num;
            p2=p1;
        }
        p1=new Node;
        p2->next=p1;
        p1->last=p2;
        p1->next=NULL;
        p1=head->next;
        while (p1->next!=NULL) {
            if (p1->num%2 == 1) {
                p1->last->next=p1->next;
                p1->next->last=p1->last;
                p2=p1->next;
                delete p1;
                p1=p2;
            }
            else p1=p1->next;
        }
        p1=head->next;
        int flag=1;
        while (p1->next!=NULL) {
            if (flag==0) cout << " ";
            flag=0;
            cout << p1->num;
            p1=p1->next;
        }
        if (flag==0) cout << endl;
    }
    return 0;
}


#include <iostream>
using namespace std;
struct Node {
    int num;
    Node *next;
};
int main() {
    Node *p1,*p2,*head;
    p1=p2=new Node;
    head=p1;
    int num;
    while (cin >> num,num!=-1) {
        p1=new Node;
        p2->next=p1;
        p1->num=num;
        p2=p1;
    }
    p1=new Node;
    p2->next=p1;
    p1->next=NULL;
    p1=head;
    while (p1->next!=NULL) {
        if (p1->next->num%2 == 1) {
            p2=p1->next;
            p1->next=p1->next->next;
            delete p2;
        }
        else p1=p1->next;
    }
    p1=head->next;
    int flag=1;
    while (p1->next!=NULL) {
        if (flag==0) cout << " ";
        flag=0;
        cout << p1->num;
        p1=p1->next;
    }
    if (flag==0) cout << endl;
    return 0;
}
posted @ 2016-05-23 23:43  vayhang_E  阅读(206)  评论(0编辑  收藏  举报