Shovels and Swords 二元一次方程

Shovels and Swords

题目

Polycarp plays a well-known computer game (we won’t mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has aa sticks and bb diamonds?InputThe first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
The only line of each test case contains two integers aa and bb (0≤a,b≤1090≤a,b≤109) — the number of sticks and the number of diamonds, respectively.OutputFor each test case print one integer — the maximum number of emeralds Polycarp can earn.Example

Input

4
4 4
1000000000 0
7 15
8 7

Output

2
0
7
5

NoteIn the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.

题意

一把铲子两个木棍一个钻石
一把剑两个钻石一个木棍
给你木棍和钻石数量
求最多一共有多少剑和铲子

题解

列方程
设可以做a把剑,b把铲子
则a+2b=木棍数 2a+b=钻石数
答案=(木棍+钻石)/3

当然这个程序在样例就过不了。。。
看样例
4
4 4
10000 0
15 7
8 7
根据我们的算法得出答案
2
3333
7
5

明显第二个答案不对
所以加一个特判

if(x==0 || y==0)cout<<0<<endl;

然后 我们提交会发现答案错误
那是因为
像这个样例 18 6
我们的答案是8
但实际是6(6把铲子)
所以还要有一个特判

  if(x<y/2)cout<<x<<endl;
  else if(y<x/2)cout<<y<<endl;

因此最终代码如下(已AC)

代码

#include
using namespace std;
int a,b,t,x,y;
int main(){
 cin>>t;
 while(t--){
  cin>>x>>y;
  if(x==0 || y==0)cout<<0<<endl;
  else if(x<y/2)cout<<x<<endl;
  else if(y<x/2)cout<<y<<endl;
  else cout<<(x+y)/3<<endl;
 }
 return 0;
}

你可能感兴趣的:(Shovels and Swords 二元一次方程)