PTA fzu_oop_east

GitHub連接: 傳送門ios

5-1該日是該年的第幾天

定義一個日期類Date,內有數據成員年、月、日,另有成員函數:構造函數用於初始化數據成員,輸出,閏年的判斷。 編寫主函數:建立日期對象,計算並輸出該日是該年的第幾天。 輸入格式: 測試輸入包含若干測試用例,每一個測試用例佔一行。當讀入0 0 0時輸入結束,相應的結果不要輸出。git

輸入樣例:github

2006 3 5
2000 3 5
0 0 0編程

輸出樣例:(括號內爲說明)函數

64 (2006年3月5日是該年的第64天)
65 (2000年3月5日是該年的第65天)oop

#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宿舍誰最高?

學校選拔籃球隊員,每間宿舍最多有4我的。現給出宿舍列表,請找出每一個宿舍最高的同窗。定義一個學生類Student,有私有成員身高height,體重weight等。測試

輸入格式:spa

首先輸入一個整型數n (1<=n<=1000000),表示n位同窗。
緊跟着n行輸入,每一行格式爲:宿舍號,name,height,weight。
宿舍號的區間爲[0,999999], name 由字母組成,長度小於16,height,weight爲正整數。code

輸出格式:對象

按宿舍號從小到大排序,輸出每間宿舍身高最高的同窗信息。題目保證每間宿舍只有一位身高最高的同窗。

輸入樣例:

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 範圍內約數最多的數

給定任意一個整數,能夠找出這個數的全部約數,如6,它有一、二、三、6一共4個約數。
咱們會給出一個整數範圍,須要你找出這個範圍內的整數中,約數最多的那個數,並輸出這個數的全部約數。

輸入格式:

輸入只有一行,共兩個正整數F,T其中1<=F<=T<=100000。

輸出格式:

輸出有兩行。
第一行由三部分組成:第一部分是「[F,T]」;第二部分是區間範圍內有最多約數的那個數;第三部分是約數的個數。三部分之間用一個空格隔開,行尾沒有空格。
第二行是對應的約數,從小到大排列,中間用一個空格隔開,末尾沒有空格。
當符合要求的答案不惟一時,輸出有最多約數時自己數最小的那個。

輸入樣例:

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

鏈表節點定義爲: struct Node{ int data; struct Node *next; }
編程實現:輸入一個正整數 repeat (0<repeat<10),作 repeat 次下列運算: 輸入若干個正整數(輸入-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爲結束標誌),創建一個單向鏈表,將其中的偶數值結點刪除後輸出。鏈表節點定義爲: struct Node{ int data; struct Node *next; }
輸入輸出示例:括號內爲說明

輸入樣例:

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;
}
相關文章
相關標籤/搜索