poj 1852 蚂蚁问题,很经典,实际上就是找最靠近边缘和最靠近中间,靠近边缘要注意小数的处理

本文介绍了一个使用C++编写的程序,该程序读取一系列整数并计算这些整数相对于中心位置的最大偏离距离及最接近中心位置的距离。通过对输入数据的处理,程序展示了如何使用条件判断和循环来解决问题。
#include<iostream>
#include<cmath>
int a[1000010];
using namespace  std;
int  main()
{
	int n,len,num,i,j,t,temp1,temp2;
	cin>>n;
	for(i=0;i<n;i++)
	{    
         cin>>len>>num;
		 for(j=0;j<num;j++)
		 {
			 cin>>a[j];
		 }
		 temp1=0;
         for(j=0;j<num;j++)
		 {
			 if(a[j]>=(len-a[j]))
				 t=a[j];
			 else
				 t=len-a[j];
			 if(t>temp1)
				 temp1=t;
		 }
		  temp2=abs(a[0]*10-(len*5));
         for(j=0;j<num;j++)
		 {
			 t=abs(a[j]*10-(len*5));
			 if(t<temp2)
				 temp2=t;
		 }
		 temp2=(len*5)-temp2;
		 temp2=temp2/10;
		 cout<<temp2<<" "<<temp1<<endl;
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值