Codeforces Round #478 (Div. 2)
B.Mancala
思路:其实这就是个模拟题,但是得分清楚。总共有14个坑的话,超过14归于0,那么应该使用mod函数。除此之外的是循环问题,一层循环相当于n-14,循环了几层其他数就相当于加了几,因此加的数字相当于n/14。
代码:
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a[14];
for(int i = 0; i < 14; i++)
cin>>a[i];
long long res = 0;
int t[14];
for(int i = 0; i < 14; i++)
{
for(int j = 0; j < 14; j++)
t[j] = a[j];
long long tmp = t[i];
t[i] = 0;
for(int j = 0; j < 14; j++)
t[j] += tmp / 14;
tmp %= 14;
int k = i + 1;
while(tmp--)
{
if(k == 14)
k = 0;
t[k]++;
k++;
}
long long score = 0;
//接下来求偶数的总和
for(int j = 0; j < 14; j++)
{
if(t[j] & 1)
continue;
score += t[j];
}
res = max(res, score);
}
cout << res << endl;
return 0;
}
在求偶数的和这一部分中,其实是有技巧的:
long long score = 0;
//接下来求偶数的总和
for(int j = 0; j < 14; j++)
{
if(t[j] & 1)
continue;
score += t[j];
}
通过位运算来提高效率,很不错的方法。