[1306] Divide The Apples 平分重量 平分数字

  • [1306] Divide The Apples

  • 时间限制: 3000 ms 内存限制: 65535 K
  • 问题描述
  • XP and EG both like eating apple. This day they bought a lot of apples. Then it was time for them to seperate these apples. They didn't want to get less apples than each other and cut the apples into pieces either. So they would get equal weight of apples. After they seperated the apples, the rest would be throw away. for example they bought 5 apples, their weight are 1 2 3 5 6 , XP will get apple of weight 1 2 and EG 3 , the rest of 5,6 will be throw away. The total weight they get is 3. But there is another better way. XP get the apples of weight 2 6 and EG 3 5 , the total weight the get is 8, and this is the best way. But they didn't know how to seperate the apples. So they ask you for help to get as many apples as possible.
  • 输入
  • The first contain a positive integer n indicate the number of apple(1<=n<=100). you can sure the total weight of the apple won't exceed 2000.
    The second line contain n integers indicate the weight of each apple.
  • 输出
  • A integer m,indicate the maximal weight XP and EG can get.
  • 样例输入
  • 5
    1 2 3 5 6
  • 样例输出
  • 8
  • 提示
  • 来源
  • 加多宝凉茶
  • 操作

给你 n 只苹果的质量,让你把它们平均分成两堆,求能平均分成两堆的最大值    可以扔掉某些苹果

对于每一个苹果,要么给 XP ,要么给 EG ,那么f[i][j] 表示 XP 拿质量为 iEG 拿质量为 j 的苹果能否成立。

那么我们就能得到如下的状态转移方程:

f[i][j] = (f[i - a[k]][j] || f[i][j - a[k]]);


#include<stdio.h>
#include<string.h>
int vis[2000][2000];
int n,a[1000];
int main()
{
    int i,j;
	while(scanf("%d",&n)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		int sum=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		vis[0][0]=1;
		sum=sum/2;
		int k;
		for(k=0;k<n;k++)
		{
			
			for(i=sum;i>=0;i--)
			{
				for(j=sum;j>=0;j--)
			//	for(j=0;j<=sum;j++)
				{
					if((i>=a[k]&&vis[i-a[k]][j])||(j>=a[k]&&vis[i][j-a[k]]))
						vis[i][j]=1;
				}
			}
		}
		for(i=sum;i>=0;i--)
		{
			if(vis[i][i]) 
			{
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The Cortex-M0 processor does not have a hardware divider, which means that division calculations are performed using software routines. There are various algorithms for performing software division, but one commonly used method is called "long division". In long division, the divisor is repeatedly subtracted from the dividend until the remainder is less than the divisor. The number of times the divisor is subtracted is the quotient, and the remainder is the final result. This process is repeated until all digits of the dividend have been processed. Here is a sample code for performing integer division on Cortex-M0 using long division: ``` int divide(int dividend, int divisor) { int quotient = 0, remainder = 0; int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1; // convert both operands to positive if (dividend < 0) dividend = -dividend; if (divisor < 0) divisor = -divisor; // perform long division for (int i = 31; i >= 0; i--) { remainder <<= 1; // left shift remainder remainder |= (dividend >> i) & 1; // add next bit from dividend to remainder if (remainder >= divisor) { remainder -= divisor; quotient |= (1 << i); // set corresponding bit in quotient } } // apply sign quotient = sign * quotient; return quotient; } ``` Note that this code assumes that both the dividend and divisor are 32-bit integers. It also handles negative operands correctly and applies the correct sign to the result. However, it may not be the most efficient implementation and may need to be optimized for specific use cases.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值