题意:1,2,3,4,5。。。加上加减号使数字和为N,数字越少越好
View Code
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n==0)
{
printf("0\n");
return 0;
}
if(n<0)n=-n;
int i;
for(i=1;i<99999;i++)
{
__int64 all=(i+1)*i/2;
if(all==n)
{
printf("%d\n",i);
break;
}
else if(all>n)
{
if((all-n)%2==0)
{
printf("%d\n",i);break;
}
if(i%2==0)
printf("%d\n",i+1);
else
printf("%d\n",i+2);
break;
}
}
return 0;
}