2021基础编程练习5-结构体扩展练习
2021基础编程练习5-结构体扩展练习
作业统计
http://oj.61coding.cn/problem.php?cid=1029&pid=0
#include<bits/stdc++.h>
using namespace std;
struct worktime{// 声明一个结构体类型 worktime 记录学生完成作业的时间
int hr,minut,sec; // hr,minut,sec 分别代表时分秒
worktime operator +(const worktime x){ // 对 + 进行重新定义
worktime tmp;
tmp.sec = (sec + x.sec) % 60;
tmp.minut = (minut + x.minut + (sec + x.sec) / 60) % 60;
tmp.hr = hr + x.hr + (minut + x.minut + (sec + x.sec) / 60) / 60;
return tmp;
}
};
int main(){
worktime stu,sum;
int n;
cin >> n;
sum.hr = sum.minut = sum.sec = 0;
for(int i = 1; i <= n; i++){
cin >> stu.hr >> stu.minut >> stu.sec;
sum = sum + stu;// 两个结构体 sum 和 stu 通过重载 + 运算符进行加法运算
}
cout << sum.hr <<"hour "<< sum.minut <<"minute "<< sum.sec <<"second";
return 0;
}
身高问题
http://oj.61coding.cn/problem.php?cid=1029&pid=1
#include<bits/stdc++.h>
using namespace std;
struct stu{
string name;
int heigh;
int num;
void input(){
cin >> name >> heigh >> num;
}
void output(){
cout << name <<" "<< heigh <<" "<< num << endl;
}
};
stu a[110];
int main(){
int n;
stu maxn;
maxn.heigh = maxn.num = 0;
cin >> n;
for(int i = 1; i <= n; i++){
a[i].input();
if(a[i].heigh > maxn.heigh) maxn = a[i];
else if(a[i].heigh == maxn.heigh && a[i].num < maxn.num) maxn = a[i];
}
maxn.output();
return 0;
}
生日相同问题
http://oj.61coding.cn/problem.php?cid=1029&pid=2
#include<bits/stdc++.h>
using namespace std;
struct node{
string name;
int mon,day;
};
node a[311];
bool cmp(node x,node y){
if (x.mon!=y.mon) return x.mon<y.mon;
else if (x.day!=y.day) return x.day<y.day;
else if (x.name.size()!=y.name.size()) return x.name.size()<y.name.size();
else return x.name<y.name;
}
int main(){
int n;
// freopen("D:\\temp\\birthday.in","r",stdin);
// freopen("D:\\temp\\birthday.out","w",stdout);
cin>>n;
for (int i=1;i<=n;i++){
cin>>a[i].name>>a[i].mon>>a[i].day;
}
sort(a+1,a+n+1,cmp);
int start=1,tot=1,flag=0;
for (int i=2;i<=n;i++){
if (a[i].mon==a[i-1].mon&&a[i].day==a[i-1].day) tot++;
else {
if (tot>1){
flag=1;
cout<<a[start].mon<<" "<<a[start].day<<" ";
for (int j=start;j<i-1;j++)
cout<<a[j].name<<" ";
cout<<a[i-1].name<<endl;
}
tot=1;
start=i;
}
}
if (tot>1){
flag=1;
cout<<a[start].mon<<" "<<a[start].day<<" ";
for (int j=start;j<n;j++)
cout<<a[j].name<<" ";
cout<<a[n].name<<endl;
}
if (flag==0) cout<<"None"<<endl;
return 0;
}
集合运算
http://oj.61coding.cn/problem.php?cid=1029&pid=3
#include<bits/stdc++.h>
using namespace std;
struct tset{
bool set[26];//集合
void input()//输入集合成员函数
{
string s;
cin >> s;
memset(set,false,sizeof(set));
for(int i = 0; i < s.size(); i++)
set[s[i] - 'a'] = true;
}
void output()//输出集合成员函数
{
for(int i = 0; i < 26; i++)
if(set[i]) cout << char(i + 'a');
cout << endl;
}
tset operator + (const tset x) const//重载+
{
tset tmp;
for(int i = 0; i < 26; i++)
tmp.set[i] = set[i] || x.set[i];
return tmp;
}
tset operator - (const tset x) const//重载-
{
tset tmp;
for(int i = 0; i < 26; i++)
tmp.set[i] = set[i] && (!x.set[i]);
return tmp;
}
tset operator * (const tset x) const//重载*
{
tset tmp;
for(int i = 0; i < 26; i++)
tmp.set[i] = set[i] && x.set[i];
return tmp;
}
};
int n;
tset a,b,c;
char op;
int main(){
// freopen("set.in","r",stdin);
// freopen("set.out","w",stdout);
cin >> n;
for(int i = 0; i < n; i++){
a.input();
cin >> op;
b.input();
if(op == '+') c = a + b;
else if(op == '-') c = a - b;
else if(op == '*') c = a * b;
c.output();
}
return 0;
}
作者:newcode 更多资源请关注纽扣编程微信公众号

从事机器人比赛、机器人等级考试、少儿scratch编程、信息学奥赛等研究学习

浙公网安备 33010602011771号