PTA fzu_oop_east

扫码查看

GitHub链接: 传送门

5-1该日是该年的第几天

输入样例:

输出样例:(括号内为说明)

#include<iostream>
#include<cstdio>
using namespace std;

class Data{
    private:
        int year,month,day;
    public:
        Data();
        bool IsLeap(int year);
        void set(int x,int y,int z);
        int TheDay(int Year,int Month,int Day);
        void Print(int Day);
};

Data::Data()
{
    year = 0;
    month = 0;
    day = 0;
}

bool Data::IsLeap(int year)
{
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    {
        return true;
    }
    else
    {
        return false;
    }
}

void Data::set(int x,int y,int z)
{
    year = x;
    month = y;
    day = z;
}

int Data::TheDay(int Year,int Month,int Day)
{
    int ans[13] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
    Day += ans[Month];
    if (IsLeap(Year))
    {
        if (Month > 2)
        {
            Day++;
        }
    }
    return Day;
}

void Data::Print(int Day)
{
    printf("%d\n",Day);
}

int main()
{
    int Year,Month,Day;
    Data Calender;
    while (~scanf("%d %d %d",&Year,&Month,&Day) && Year && Month && Day)
    {
        Calender.set(Year,Month,Day);
        Day = Calender.TheDay(Year,Month,Day);
        Calender.Print(Day);
    }
    return 0;
}

5-2宿舍谁最高?

输入格式:

输出格式:

输入样例:

7
000000 Tom 175 120
000001 Jack 180 130
000001 Hale 160 140
000000 Marry 160 120
000000 Jerry 165 110
000003 ETAF 183 145
000001 Mickey 170 115

输出样例:

000000 Tom 175 120
000001 Jack 180 130
000003 ETAF 183 145
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include <iomanip>
using namespace std;

class Student
{
    private:
        int height;
        int weight;
        string name;
    public:
        void memset();
        void set(int h,int w,string ne);
        void cmp(int h,int w,string ne);
        void print();
};

void Student::set(int h,int w,string ne)
{
    height = h;
    weight = w;
    name = ne;
}

void Student::memset()
{
    height = -1;
    weight = -1;
}
void Student::cmp(int h,int w,string ne)
{
    if (h > height)
    {
        set(h,w,ne);
    }
}

void Student::print()
{
    cout << name << " " << height << " " << weight << endl;
}

Student stu[1000005];
int cnt[1000005];

int main()
{
    int N;
    for (int i = 0;i < 1000000;i++)
    {
        stu[i].memset();
    }
    scanf("%d",&N);
    int h,w,roomnum;
    string ne;
    for (int i = 0;i < N;i++)
    {
        cin >> roomnum >> ne >> h >> w;
        stu[roomnum].cmp(h,w,ne);
        cnt[i] = roomnum;
    }

    sort(cnt,cnt + N);

    for (int i = 0;i < N;)
    {
        while (cnt[i] == cnt[i + 1])
        {
            i++;
        }
        cout << setfill('0') << setw(6) << cnt[i] << " ";
        stu[cnt[i++]].print();
    }
}

5-3 范围内约数最多的数

输入格式:

输出格式:

输入样例:

3 10

输出样例:

[3,10] 6 4
1 2 3 6
#include<iostream>
#include<set>
#include<map>
#include<cstdio>
#include<cmath>
using namespace std;

int Factor(int N)
{
    set<int>all;
    int max = sqrt(N);
    for (int i = 1;i <= max;i++)
    {
        if (N % i == 0)
        {
            all.insert(i);
            all.insert(N/i);
        }
    }
    int len = all.size();
    return len;
}

int main()
{
    int F,T,num,max = 0;
    set<int>all;
    set<int>::iterator it;
    scanf("%d%d",&F,&T);
    for (int i = F;i <= T;i++)
    {
        int len = Factor(i);
        if (max < len)
        {
            num = i;
            max = len;
        }
        else if (max == len && i < num)
        {
            num = i;
            max = len;
        }
    }
    max = sqrt(num);
    for (int i = 1;i <= max;i++)
    {
        if (num % i == 0)
        {
            all.insert(i);
            all.insert(num/i);
        }
    }
    printf("[%d,%d] %d %d\n",F,T,num,all.size());
    bool first = true;
    for (it = all.begin();it != all.end();it++)
    {
        first?printf("%d",*it):printf(" %d",*it);
        first = false;
    }
    return 0;
}

5-4 单向链表1

输入样例:

2 (repeat=2)
1 2 3 4 5 6 7 -1
1 3 5 -1

输出样例:

2 4 6
#include<stdio.h>
#include<stdlib.h>

struct Node{
    int data;
    struct Node *next;
};

struct Node *Init()
{
    struct Node *current,*prev;
    struct Node *head = NULL;
    current = (struct Node *)malloc(sizeof(struct Node));
    scanf("%d",&current->data);

    while (current->data != -1)
    {
        if (current->data % 2 != 0)
        {
            current = (struct Node *)malloc(sizeof(struct Node));
            scanf("%d",&current->data);
            continue;
        }
        if (head == NULL)
        {
            head = current;
        }
        else
        {
            prev->next = current;
        }
        prev = current;
        prev->next = NULL;

        current = (struct Node *)malloc(sizeof(struct Node));
        scanf("%d",&current->data);
    }
    return head;
}

void Print(struct Node *head)
{
    bool first = true;
    bool empty = true;
    struct Node *current = head;
    while (current != NULL)
    {
        first?printf("%d",current->data):printf(" %d",current->data);
        first  = false;
        empty = false;
        current = current->next;
    }
    if (!empty)
    {
        printf("\n");
    }
}

void Free(struct Node *head)
{
    struct Node *current,*tmp;
    current = head;
    while (current != NULL)
    {
        tmp = current->next;
        free(current);
        current = tmp;
    }
}

int main()
{
    struct Node *head = NULL;
    int repeat;
    scanf("%d",&repeat);
    while (repeat--)
    {
        head = Init();
        Print(head);
        Free(head);
    }
    return 0;
}

5-5 链表操作2

输入样例:

1 2 3 4 5 6 7 -1

输出样例:

1 3 5 7
#include<stdio.h>
#include<stdlib.h>

struct Node{
    int data;
    struct Node *next;
};

struct Node *Init()
{
    struct Node *current,*prev;
    struct Node *head = NULL;
    current = (struct Node *)malloc(sizeof(struct Node));
    scanf("%d",&current->data);

    while (current->data != -1)
    {
        if (current->data % 2 == 0)
        {
            current = (struct Node *)malloc(sizeof(struct Node));
            scanf("%d",&current->data);
            continue;
        }
        if (head == NULL)
        {
            head = current;
        }
        else
        {
            prev->next = current;
        }
        prev = current;
        prev->next = NULL;

        current = (struct Node *)malloc(sizeof(struct Node));
        scanf("%d",&current->data);
    }
    return head;
}

void Print(struct Node *head)
{
    bool first = true;
    bool empty = true;
    struct Node *current = head;
    while (current != NULL)
    {
        first?printf("%d",current->data):printf(" %d",current->data);
        first  = false;
        empty = false;
        current = current->next;
    }
    if (!empty)
    {
        printf("\n");
    }
}

void Free(struct Node *head)
{
    struct Node *current,*tmp;
    current = head;
    while (current != NULL)
    {
        tmp = current->next;
        free(current);
        current = tmp;
    }
}

int main()
{
    struct Node *head = NULL;
    head = Init();
    Print(head);
    Free(head);
    return 0;
}
05-11 11:15
查看更多