- 实验内容、实验步骤及实验结果
题目1
例如:
7-1 平均成绩
代码清单:
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
string str;
int m_a;
int m_b;
int m_c;
double avg;
void set(string str,int a,int b,int c)
{
this->str=str;
this->m_a=a;
this->m_b=b;
this->m_c=c;
this->avg=(a+b+c)*1.0/3;
}
};
int main()
{
int i=0;
string str;
int a,b,c;
int n;
cin>>n;
student *p=new student[n];
for(i=0;i<n;i++)
{
cin>>str>>a>>b>>c;
p[i].set(str,a,b,c);
}
for(i=n-1;i>=0;i--)
{
if(p[i].avg>=60)
{
cout<<p[i].str<<" "<<p[i].m_a<<" "<<p[i].m_b<<" "<<p[i].m_c;
printf(" %.2lf\n",p[i].avg);
}
}
return 0;
}
运行结果截图
题目2(给出题目描述)
7-2 队列操作
代码清单:
#include<iostream>
using namespace std;
class MyQueue{
private:
int p[10001];
int l=0;
int r=0;
public:
void push(int x);
int pop()
{
return p[l++];
}
int size()
{
return r-l;
}
};
void MyQueue::push(int x)
{
p[r++]=x;
}
int main()
{
int n,i;
MyQueue my;
cin>>n;
for(i=0;i<n;i++)
{
int op;
cin>>op;
if(op==1)
{
int x;
cin>>x;
my.push(x);
}
else if(op==2)
{
if(my.size()==0)
cout<<"Invalid"<<endl;
else
cout<<my.pop()<<endl;
}else if(op==3)
{
cout<<my.size()<<endl;
}
}
return 0;
}
运行结果截图
题目3(给出题目描述)
7-3 鸿鸿哥的苹果树
代码清单:
#include<iostream>
using namespace std;
int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cin>>a[i];
}
int x=0;
cin>>x;
int count=0;
for(int i=0;i<10;i++)
{
if((x+30)>=a[i])
count++;
}
cout<<count;
return 0;
}
运行结果截图