leetcode:1276. 不浪费原料的汉堡制作方案(贪心)

题目:

在这里插入图片描述

分析:

第一想法:紫书上的数学模块判断有无整数解。
4x+2y=番茄总数
x+y=奶酪总数
嗯。解方程。

结果:整数+正数

代码:

 int tomatoSlices;
 int cheeseSlices;
 int x,y;
 vector<int> v;
 if((tomatoSlices-2*cheeseSlices)%2==0)
 {
  x=tomatoSlices-2*cheeseSlices/2;
  y=cheeseSlices-x;
  if(x<=||y<0) return v;
  v.push_back(x);
  v.push_back(y);
  return v;
 }
 return v;
发布了48 篇原创文章 · 获赞 20 · 访问量 598

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/104098855