Joysticks
题意
求游戏最多能坚持多长时间
思路
- 因要求最长时间,所以给每个少的电量冲电
- 要注意a1,a2不能一方为0,a1,a2不能都小于2
- 运用for循环和if判断进行运算
坑点
- 注意特殊情况a1,a2为0时
- xa1,a2都雄安与2时
算法一:for,while循环
时间复杂度
入门
实现步骤
- 先把两个特殊情况进行运算
- while循环,当a1,a2都大于0时
- 输出分钟
代码
#include <iostream>
using namespace std;
int main(){
int a1,a2;
cin>>a1>>a2;
int m=0;//分钟
if(a1<2 && a2<2){
cout<<"0";
}
else if(a1<1 || a2<1){
cout<<"0";
}//以上两种情况都为0分钟
else{
while(a2>0 && a1>0 ){//注意符号为&&
if(a1<a2){
a1=a1+1;
a2=a2-2;
m++;
}
else if(a2<a1){
a2=a2+1;
a1=a1-2;
m++;
}
else if(a1=a2){
a1=a1+1;
a2=a2-2;
m++;
}
}
cout<<m;
}
return 0;
}