打倒魔王↖(^ω^)↗
时间: 1ms 内存:128M
描述:
从前有一个王子,他喜欢上了邻国的一个公主。终于有一天他向公主表白了,公主答应了他。
在婚礼那天,可恶的魔王来了,他抢走了公主。王子很愤怒(╰_╯)#,他决定打上魔宫救回公主。
临走那天,巫师给了他一本魔法书,让他可以看到自己与敌人的差距,来促使他不断进步。
当王子可以打败怪物时魔法书显示:Trial!,当打不败时显示:You need more power!
怪物的等级比较低,只有被攻击时才会反击,王子会不断地进行攻击,每攻击一次,怪物就反击一次,直到怪物被杀死,或者被怪物杀死。
损失的血量 = 敌人的攻击-自己的防御//以下代码会自动添加到程序的开始
#include<iostream>
using namespace std;
class Role
{
public:
friend void judge(Role &,Role &);
void input()
{
cin>>HP>>ATK>>DEF;
}
private:
int HP;
int ATK;
int DEF;
};//以下请给出 成员函数judge
//以下代码会自动添加到程序的末尾
int main()
{
Role monster,prince;
monster.input();
prince.input();
judge(monster,prince);
return 0;
}
输入:
输入有多行
第一行:怪物的属性:血量,攻击,防御;
第二行:王子的属性:血量,攻击,防御。
输出:
打不过怪物时输出:You need more power!,否则输出:Trial!。
示例输入:
9 6 2
8 5 3
示例输出:
Trial!
提示:
参考答案(内存最优[1092]):
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int gx,gg,gf,wx,wg,wf;
scanf("%d%d%d",&gx,&gg,&gf);
scanf("%d%d%d",&wx,&wg,&wf);
while(gx>0&&wx>0)
{
gx=gx+gf-wg;
if(gx<=0)goto out;
else(wx=wx+wf-gg);
}out:
if(wx>gx)printf("Trial!");
else printf("You need more power!");
return 0;
}
参考答案(时间最优[0]):
#include<iostream>
using namespace std;
class Role
{
public:
friend void judge(Role &,Role &);
void input()
{
cin>>HP>>ATK>>DEF;
}
private:
int HP;
int ATK;
int DEF;
};
void judge(Role &monster,Role &prince)
{
int hpm=monster.HP,hpl=prince.HP;
int T_A_m=monster.ATK-prince.DEF; //王子每次损失的血量
int T_A_l=prince.ATK-monster.DEF; //魔王每次损失的血量
if(prince.ATK<=monster.DEF)
{
cout<<"You need more power"<<endl;
return ;
}
if(monster.ATK<=prince.DEF)
{
cout<<"Trial!"<<endl;
return ;
}
while(hpm&&hpl)
{
hpm-=T_A_l;
if(hpm<=0)
break;
hpl-=T_A_m;
}
if(hpl>0)
cout<<"Trial!"<<endl;
else
cout<<"You need more power!"<<endl;
}
int main()
{
Role monster,prince;
monster.input();
prince.input();
judge(monster,prince);
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。
