cyyyyyyyyy


私信TA

用户名:chenyuQAQ

访问量:1042

签 名:

等  级
排  名 10861
经  验 1009
参赛次数 0
文章发表 8
年  龄 0
在职情况 学生
学  校 南昌工程学院
专  业

  自我简介:

TA的其他文章

解题思路:  按照题目模拟便可

注意事项:

参考代码:

#include<stdio.h>
int count=0; //用于统计添加的糖果数目并设为全局变量。
int check(int a[],int n);
void distribute(int a[],int n);     //建议写函数用这种 先于main函数前面声明 后于main函数定义 的方式
void add(int a[],int n);
int main()
{
	int n;
	scanf("%d",&n);
	int i,a[n];
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	while(1)//写一个while死循环,直到糖果都相同
	{
		if(check(a,n)==1)
		{
		  break;
		}
		distribute(a,n);
		add(a,n);
	}
	printf("%d",count);//其实这行代码可写到上面if语句中的break前面,判断糖果都相同时便可打印出来。
	return 0;
}
int check(int a[],int n)//判断糖果是否都相同
{
	int temp=a[0];
	int i;
	for(i=1;i<n;i++)
	{
		if(a[i]!=a[0])
		{
			return 2;
		}
	}
	return 1;
}
void distribute(int a[],int n)//实现分配
{
	int i,temp;
	for(i=0;i<n;i++)
	{
		if(i==0)
		{
			temp=a[i]/2;
			a[i]/=2;
		}
		else if(i!=0&&i!=n-1)
		{
			a[i-1]+=a[i]/2;
			a[i]/=2;
		}
		else if(i==n-1)
		{
			a[i-1]+=a[i]/2;
			a[i]/=2;
			a[i]+=temp;
		}
	}	
}
void add(int a[],int n)//为奇数的便加一颗糖果
{
	int i;
	for(i=0;i<n;i++)
	{
		if(a[i]%2!=0)
		{
			a[i]++; 
			count++;
		}
	}
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区