CF245A 题解
\(\color{red}{see}\space \color{green}{in}\space \color{blue}{my}\space \color{purple}{blog}\)
小学生又双叒叕来写题解啦!
这题难度并不高,是一个简单的模拟。
题目大意:
-
有两台服务器。
-
向服务器发送信息,信息分为两种:发送成功与发送失败。
-
最后计算服务器是“活的”还是“死的”。
解题思路:
在此为萌新隆重推出:结构体!
结构体定义如下:
//定义一个名字叫 QAQ 的结构体。
struct QAQ
{
//可以在结构体内定义各种类型的变量。
int a, b;
bool c;
}; //注意末尾的分号不要漏掉。
QAQ x, y; //创建 QAQ 类型的变量。
QAQ z[233]; //也可以定义数组。
定义完了,如何使用呢?
int main()
{
cin >> x.a >> y.b; //读入。
cout << x.a << " " << y.b << " "; //输出。
x.a += 233;
cout << x.a << " ";
if (x.a == y.b) cout << "equal!";
//和变量一样,可以斌值、比较等。
return 0;
}
结构体很容易吧,那就试着用结构体做这一题!
我们用结构体记录两个服务器的状态,如下。
struct Ping
{
int live, dead;
};
Ping a, b;
对于每条信息,我们可以这样处理:
int t, x, y;
cin >> t >> x >> y;
if (t == 1) //第一台服务器。
{
a.live += x;
a.dead += y;
}
else if (t == 2) //第二台服务器。
{
b.live += x;
b.dead += y;
}
最后,判断计算机“是活是死”。
if (a.live >= a.dead) cout << "LIVE\n";
else cout << "DEAD\n";
if (b.live >= b.dead) cout << "LIVE\n";
else cout << "DEAD\n";
这样就可以轻松通过这一题啦!
前面已经讲解得很详细,完整代码便不加注释了。
完整代码:
#include <iostream>
#include <cstdio>
using namespace std;
struct Ping
{
int live, dead;
};
Ping a, b;
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
int t, x, y;
scanf("%d%d%d", &t, &x, &y);
if (t == 1)
{
a.live += x;
a.dead += y;
}
else if (t == 2)
{
b.live += x;
b.dead += y;
}
}
if (a.live >= a.dead) printf("LIVE\n");
else printf("DEAD\n");
if (b.live >= b.dead) printf("LIVE\n");
else printf("DEAD\n");
return 0;
}
首发:2022-03-26 12:52:50