Joysticks

题目名字 Joysticks

题目链接

题意

两个游戏机,保证两个游戏机都是有电的,充电的游戏机每分钟加一格电,不充电的每分钟消耗两个电

思路

  1. 比较两个游戏机的电量,电量较少的先充电即+2,另外一个减一,设置if语序如果有一个为一就去充电,并且保持两个都不为零即可

算法一:if语句

代码
 #include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;int main() {int a1,a2;cin>>a1>>a2;if(a1 == 1 && a2 == 1) {//两个都是1,冲谁都没用cout<<"0";return 0;}int cnt = 0;while(a1 > 0 && a2 > 0) {if(a1 >= a2) {a2 += 1;a1 -= 2;} else {a2 -= 2;a1 += 1;}if(a1 >= 0 && a2 >= 0) cnt++;}cout<<cnt;return 0;
}