原代码: (通过率为100%)
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct team
{ int n;
int p;
int s;
};
struct team t1,t2;
int main()
{
cin>>t1.n>>t1.p>>t1.s;
cin>>t2.n>>t2.p>>t2.s;
if(t1.n<t2.n)
{
cout<<'2'<<endl;
}
else if(t1.n>t2.n)
{
cout<<'1'<<endl;
}
else{
if(t1.p<t2.p)
{
cout<<'1'<<endl;
}
else if(t1.p>t2.p)
{
cout<<'2'<<endl;
}
else{
if(t1.s<t2.s)
{
cout<<'1'<<endl;
}
else if(t1.s>t2.s)
{
cout<<'2'<<endl;
}
else
{
cout<<"God"<<endl;
}
}
}
return 0;
}
以下关于结构体内容来自:c.biancheng.net/view/2031.html
理论上讲结构体的各个成员在内存中是连续存储的,和数组非常类似,例如上面的结构体变量 stu1、stu2 的内存分布如下图所示,共占用 4+4+4+1+4 = 17 个字节。
需要注意的是,结构体是一种自定义的数据类型,是创建变量的模板,不占用内存空间;结构体变量才包含了实实在在的数据,需要内存空间来存储。
通过率为100%代码:
基本知识点:用sort(含定义cmp子函数)排序比较结构体大小
#include<iostream>
using namespace std;
#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define minx 1e18;
#define maxx 0;
typedef long long ll;
struct st{
int n,p,s;
int d;
};
st stu[2];
int cmp(st a,st b)
{
if(a.n!=b.n)return a.n>b.n;
else if(a.p!=b.p)return a.p<b.p;
else return a.s<b.s;
}
int main()
{
stu[0].d=1;
stu[1].d=2;
cin>>stu[0].n>>stu[0].p>>stu[0].s;
cin>>stu[1].n>>stu[1].p>>stu[1].s;
sort(stu,stu+2,cmp);
if(stu[0].n==stu[1].n&&stu[0].p==stu[1].p&&stu[0].s==stu[1].s)
{
cout<<"God"<<endl;
}else{
cout<<stu[0].d<<endl;
}
return 0;
}