分糖果
题目描述:
思路:
这道题主要是两个难点
1.均分的问题,这里可以直接除,因为C++里的整数变量是下取整的,所以自动过滤掉余数。
2.怎样处理环的问题,这里我们可以进行特殊处理,即:特殊处理第1个人,第n个人。
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
ll n,a[1200];
int main()
{
//freopen("candy.in","r",stdin);
//freopen("candy.out","w",stdout);
ios::sync_with_stdio(false);
cin >> n;
for(int i = 1;i <= n;++i)
{
cin >> a[i];
}
ll x = a[1] / 3;
a[1] = x;
a[2] += x;
a[n] += x;
for(int i = 2;i <= n;++i)
{
if(i == n)
{
ll x = a[n] / 3;
a[n] = x;
a[1] += x;
a[n - 1] += x;
}
else
{
ll x = a[i] / 3;
a[i] = x;
a[i - 1] += x;
a[i + 1] += x;
}
}
for(int i = 1;i <= n;++i)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
Thanks for your watching,hope this blog can help you.