您的位置:首页 > 运维架构

PTA fzu_oop_east

2016-05-24 21:22 435 查看

GitHub链接: 传送门

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


定义一个日期类Date,内有数据成员年、月、日,另有成员函数:构造函数用于初始化数据成员,输出,闰年的判断。 编写主函数:创建日期对象,计算并输出该日是该年的第几天。 输入格式: 测试输入包含若干测试用例,每个测试用例占一行。当读入0 0 0时输入结束,相应的结果不要输出。


输入样例:


2006 3 5

2000 3 5

0 0 0


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


64 (2006年3月5日是该年的第64天)

65 (2000年3月5日是该年的第65天)


#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等。


输入格式:


首先输入一个整型数n (1<=n<=1000000),表示n位同学。

紧跟着n行输入,每一行格式为:宿舍号,name,height,weight。

宿舍号的区间为[0,999999], name 由字母组成,长度小于16,height,weight为正整数。


输出格式:


按宿舍号从小到大排序,输出每间宿舍身高最高的同学信息。题目保证每间宿舍只有一位身高最高的同学。


输入样例:

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,它有1、2、3、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",¤t->data);

while (current->data != -1)
{
if (current->data % 2 != 0)
{
current = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",¤t->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",¤t->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",¤t->data);

while (current->data != -1)
{
if (current->data % 2 == 0)
{
current = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",¤t->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",¤t->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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: