PTA HDU C++作业
7-1 Circle类
(Circle类)一个圆形的旱冰场地,场地内抹水泥,造价为每平方米20元,围栏用木条围成,每米造价35元。设计一个Circle类,可以求出圆的面积和边长,进而支持求出旱冰场的等价。请在下面提示的基础上完成程序,其中需要做的工作包括:(1)在类声明中声明相关的公共成员函数;(2)在类声明和main()函数之间,定义声明的成员函数。PI取3.1415926。
输入格式:
请输入旱冰场的半径
输出格式:
旱冰场的造价,小数点后保留2位
输入样例:
5.5
输出样例:
3110.18
代码:
#include<iostream>
#include<iomanip>
using namespace std;
double PI = 3.1415926;
class Circle{
public:
Circle(double r);
~Circle(){}
double GetS();
double GetC();
private:
double r;
};
Circle::Circle(double r){
this -> r = r;
}
double Circle::GetS(){
return PI*r*r;
}
double Circle::GetC(){
return 2*PI*r;
}
int main(){
double r;
cin>>r;
Circle circle(r);
//cout<<circle.GetS()<<" "<<circle.GetC()<<endl;
double Price = 20 * circle.GetS() + 35 * circle.GetC();
cout<<fixed<<setprecision(2)<<Price;//C++小数点后保留两位
return 0;
}
7-2 奇数阶魔阵问题
对于每一个正整数奇数n,输出一个n X n矩阵(奇数阶魔阵), 矩阵元素由1~n平方整数组成, 矩阵的每一行之和、每一列之和、主对角线之和均相等。要求使用动态存储分配.
输入格式:
正整数奇数n
输出格式:
每个阵列中整数占5位
输入样例:
在这里给出一组输入。例如:
7
输出样例:
在这里给出相应的输出。例如:
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
思路:
从1开始填,思考下一个数填写的位置
一、下一个要填的数的位置是默认的0时:
1) 首先,1在第0行,第n/2列的位置
2)2本应该在1的右上角,也就是 i- -, j++(上面一行,右边一列)的位置,但其实却在i=n-1,j++(最后一行,右边一列)的位置;
3)3和4正常:都在上一个数右上角的位置;
4)5本应该在4的右上角,但是这时候j=n,越界了,其实上5在这一行的j=0也就是第一列的位置;
二、下一个要填的数的位置有数字并且不是0时
5)数字6和7正常填写,数字8本应该在7的右上角,但7的右上角已经填过了,这时候我们就要从7的右上角的位置执行i=i+2; j- -;
6)数字29本应该在28的右上角,但是此时i<0且j = n,都越界,我们本应该让i=n-1, j=0 的,但是这个位置已经有22这个数字了,故,当i和j均越界的时候,我们直接令i=1,j=n-1.
代码:
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
using std::setw;
int main(){
int n;
cin>>n;
vector<int> a(n*n,0);
int i,j,count;
i=0;
j=n/2;
count=1;
while(count<=n*n){
if(a[i*n+j]==0){
a[i*n+j]=count++;
i--;
j++;
}
else{
if(i==n-1&&j==0){
i=1;
j=n-1;
}
else{
i+=2;
j--;
}
}
if(i<0)
i=n-1;
if(j==n)
j=0;
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cout<<std::setw(5)<<a[i*n+j];//默认右对齐,整数占5位
}
cout<<endl;
}
}
7-3 有序单链表合并
建立两个空单链表,输入若干正整数,将奇数插入单链表1,将偶数插入单链表2,再将两个单链表合并成一个新单链表。最后分别输出3个单链表。要求所有单链表中元素呈递增或相等排列,程序不可有内存泄漏。提示:从小到大逐个比较两个单链表中的元素,将较小的元素(相等时两个元素同时)添加到新链表的末尾,处理完一个链表后,再将另一个链表中剩余元素添加至新链表。
输入格式:
若干正整数,0代表输入结束。
输出格式:
每个单链表占一行,每个单链表输出时元素间用->分隔。
输入样例:
在这里给出一组输入。例如:
1 2 3 4 5 6 7 8 9 0
输出样例:
在这里给出相应的输出。例如:
1->3->5->7->9
2->4->6->8
1->2->3->4->5->6->7->8->9
代码:
#include<iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
void Insert(Node *p,int x); //将元素插入有序单链表中,插入后仍然有序
Node* Merge(Node *h1,Node *h2);//合并单链表
void Destroy(Node *p); //销毁单链表
void Print(Node *p); //打印单链表
int main(){
//建立三个带头节点的单链表
Node *h1=new Node;
Node *h2=new Node;
Node *h3=new Node;
h1->next = h2->next = h3->next = NULL;
int num;
while(cin>>num && num!=0){
if(num %2 ==1){
Insert(h1,num);
}else{
Insert(h2,num);
}
}
h3=Merge(h1,h2);
Print(h1);
Print(h2);
Print(h3);
Destroy(h3);//销毁单链表,避免内存泄漏
Destroy(h2);
Destroy(h1);
return 0;
}
void Insert(Node *p,int x){
//申请节点
Node *q = new Node;
q->data = x;
//查找合适插入位置
Node * temp = p;
while(temp->next && x > temp->next->data){
temp = temp->next;
}
//将节点插入temp所指的节点后
q->next = temp->next;
temp->next = q;
}
void Destroy(Node *p){
while(p){
Node *q = p->next;
delete p;
p = q;
}
}
void Print(Node *p){
p = p->next;//头节点没有数据
if(p){
cout<< p->data;
p = p->next;
}
while(p){
cout<<"->"<< p->data;
p = p->next;
}
cout<<endl;
}
Node* Merge(Node *h1,Node *h2){
Node *h3 = new Node;
Node *realh3 = new Node;
realh3 = h3;
h1 = h1->next;
h2 = h2->next;
while(h1&&h2){
Node *t = new Node;
t->next = NULL;
if(h1->data <= h2->data){
t->data = h1->data;
h1 = h1->next;
}
else{
t->data = h2->data;
h2 = h2->next;
}
h3->next = t;
h3 = h3->next;
}
while(h1){
Node *t = new Node;
t->next = NULL;
t->data = h1->data;
h3->next = t;
h3 = h3->next;
h1 = h1->next;
}
while(h2){
Node *t = new Node;
t->next = NULL;
t->data = h2->data;
h3->next = t;
h3 = h3->next;
h2 = h2->next;
}
return realh3;
}