题目:
有红黑两种积木,红积木用正数A表示,黑积木用负数B表示,将积木排成一排,每7个积木代表数字之和要小于等于0,选17个积木排成一排,求17个积木最大之和
代码1:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int sum = 0;
int flag = 1;//记录b的个数
int cur = 6 * a + b;
int res = 0;
while (cur >= 0)
{
cur = cur + b - a;
++flag;
}
flag = 7 - flag;//每7个积木中,a的个数
if (flag >= 3)
sum = 2 * cur + 3 * a;//总共有17个积木,17/7=2余3,
else
sum = 2 * cur + flag * a + (3 - flag) * b;
cout << sum << endl;
return 0;
}
代码2:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int sum = 0;
int a = 6, b = 1;
while (a*A + b*B>0) //连续7个积木之和小于0时,A的个数为a,B的个数为b
{
a--;
b++;
}
vector<int> m(7);
for (int i = 0; i<7; i++)
{
if (i<a)
m[i] = A;
else
m[i] = B;
}
int sum = 0;
for (int i = 0; i<17; i++)
{
sum += m[i % 7];
}
cout << sum << endl;
return 0;
}