Moving stones(暴力+思维)

链接:https://ac.nowcoder.com/acm/contest/5891/D
来源:牛客网

题目描述
One day, GK was getting very bored with palying stones. So he made a rule for himself:
There are n piles of stones in total, at least 0 stones in each pile. You can select a pile of stones each time you move, and then take out one stone from each other (n-1 in total) and put them into the selected pile. The number of stones in any pile at any time cannot be less than 0.That is to say, before you select a pile of stones to add to it, if there have a pile of 0 stones in other piles, you will not be able to select this pile of stones.(For example,there are 3 piles of stones,1 4 1,If you choose the second pile, it will become 0 6 0,and you won’t be able to do anything next )
GK wants to know if he can make every pile of stones equal by lots of operation as many times as he want. GK never does anything uncertain, so after a long meditation, he decides to ask for your help.
输入描述:
The first line is an integer T (1 ≤ T ≤ 1000), indicates that there are T-group data.
Each group of test data has two lines, the first line has an integer n (1≤n≤100), which means there are n piles of stones, the second line has n integers a1, a2…an (0 ≤ai ≤1000), which means the number of stones in each pile.
输出描述:
Each group of test data corresponds to an output. If GK can meet the requirements after any number of operations,print"Yes". Otherwise, print “No”.
There is no extra space at the beginning and end of the line, and each group of output takes up one line.
示例1
输入
复制
2
3
1 2 3
3
2 2 2
输出
复制
No
Yes
一开始这个题目没有任何的思路。不知道从哪一方面下手。。
对于这个序列中最小的那个数字,我们肯定是要匀给它一些石子的。经过这样若干次操作之后,在符合的条件下,就会达到平均。那么我们就模拟这个过程。这个题目数据量不是很大,就能过了。更好的方法暂时没有想到,希望路过的大佬可以指正。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e3+100;
int a[maxx];
vector<int> v;
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		sort(a+1,a+1+n);
		int sum=0;
		for(int i=1;i<=n;i++) sum+=a[i];
		if(sum%n) cout<<"No"<<endl;
		else
		{
			v.clear();
			for(int i=1;i<=n;i++) v.push_back(a[i]);
			int flag=0;
			int cnt=0;
			while(cnt<=1500)
			{
				sort(v.begin(),v.end());
				if(v[0]==v.back()) 
				{
					flag=1;
					break;
				}
				if(v[1]==0) break;//出现0了,说明就要变成负数了,这样肯定不行。
				v[0]+=(n-1);
				for(int i=1;i<n;i++) v[i]--;
				cnt++;
			}
			if(flag==0) cout<<"No"<<endl;
			else cout<<"Yes"<<endl;
		}
	}
	return 0;
}

努力加油a啊,(o)/~

这段代码中有几个问题: 1. while 循环的判断条件应该是 `stones.length > 1`,因为只有至少有两个石头时才需要进行破碎的操作。所以应该将 `while( stones.length==1 || stones.length==0 )` 修改为 `while( stones.length > 1 )`。 2. 在交换石头的位置时,应该使用临时变量来保存一个石头的值,然后再进行交换。所以应该将 `a=stones[j];` 修改为 `int temp = stones[i];`,将 `stones[i]=stones[j];` 修改为 `stones[i]=stones[j];`,将 `stones[j]=a;` 修改为 `stones[j]=temp;`。 3. 在判断最后剩下的石头重量时,应该使用索引 `stones.length-1` 和 `stones.length-2` 来获取最后两个石头的重量。所以应该将 `if(stones[stones.length-1]<=stones[stones.length])` 修改为 `if(stones[stones.length-1]<=stones[stones.length-2])`,将 `stones.length=stones.length-2;` 修改为 `stones.length -= 2;`。 4. 最后返回的应该是 `stones[0]`,而不是 `stones[stones.length]`。所以应该将 `return stones[stones.length];` 修改为 `return stones[0];`。 修正后的代码如下: ```java class Solution { public int lastStoneWeight(int[] stones) { while (stones.length > 1) { for (int i = 0; i < stones.length; i++) { for (int j = 1; j < stones.length; j++) { if (stones[i] > stones[j]) { int temp = stones[i]; stones[i] = stones[j]; stones[j] = temp; } } } if (stones[stones.length - 1] <= stones[stones.length - 2]) { stones[stones.length - 2] -= stones[stones.length - 1]; stones = Arrays.copyOf(stones, stones.length - 1); } else { stones = Arrays.copyOf(stones, stones.length - 2); } } return stones[0]; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值