Description
Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into two pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem?
Input
The first input line contains integer n (1 ≤ n ≤ 105) — amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.
Output
Output the amount of ways to cut the stripe into two non-empty pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.
Sample Input
0 0 2 0 0 1
RIGHT
2 3 4 5 6 6
NEITHER
-1 0 2 0 0 1
ALMOST
代码:
#include<iostream>
using namespace std;
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool is3(int x1,int y1,int x2,int y2,int x3,int y3)
{
int a,b,c;
a=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
b=(x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
c=(x3-x2)*(x3-x2)+(y3-y2)*(y3-y2);
if(a==0||b==0||c==0)
return 0;
if(a+b==c||a+c==b||b+c==a)
return 1;
else
return 0;
}
int main()
{
//int x1,x2,x3,y1,y2,y3;
int x1[4],y1[4];
//cin>>x1>>y1>>x2>>y2>>x3>>y3;
for(int i=0;i<3;i++)
{
cin>>x1[i]>>y1[i];
}
/*
if((x1[0]==x1[1]&&y1[0]==y1[1])||(x1[1]==x1[2]||y1[1]==y1[2])||(x1[0]==y1[2])&&(y1[0]==y1[2]))
{
cout<<"NEITHER"<<endl;
return 0;
}*/
if(is3(x1[0],y1[0],x1[1],y1[1],x1[2],y1[2]))
{
cout<<"RIGHT"<<endl;
return 0;
}
int tx,ty;
for(int i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
tx=x1[i]+move[j][0];
ty=y1[i]+move[j][1];
if(i==0&&is3(tx,ty,x1[1],y1[1],x1[2],y1[2]))
{
cout<<"ALMOST"<<endl;
return 0;
}
if(i==1&&is3(x1[0],y1[0],tx,ty,x1[2],y1[2]))
{
cout<<"ALMOST"<<endl;
return 0;
}
if(i==2&&is3(x1[0],y1[0],x1[1],y1[1],tx,ty))
{
cout<<"ALMOST"<<endl;
return 0;
}
}
if(i==3)
cout<<"NEITHER"<<endl;
}
return 0;
}
需要注意的是如果三个点重合的情况,也满足a+b=c 这种情况用
if(a==0||b==0||c==0)
return 0;
解决