题目链接:点击打开链接
One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into several parts. This time Lesha decided to split the array A into several, possibly one, new arrays so that the sum of elements in each of the new arrays is not zero. One more condition is that if we place the new arrays one after another they will form the old array A.
Lesha is tired now so he asked you to split the array. Help Lesha!
The first line contains single integer n (1 ≤ n ≤ 100) — the number of elements in the array A.
The next line contains n integers a1, a2, ..., an ( - 103 ≤ ai ≤ 103) — the elements of the array A.
If it is not possible to split the array A and satisfy all the constraints, print single line containing "NO" (without quotes).
Otherwise in the first line print "YES" (without quotes). In the next line print single integer k — the number of new arrays. In each of the next k lines print two integers li and ri which denote the subarray A[li... ri] of the initial array A being the i-th new array. Integers li, rishould satisfy the following conditions:
If there are multiple answers, print any of them.
3 1 2 -3
YES 2 1 2 3 3
8 9 -12 3 4 -4 -10 7 3
YES 2 1 2 3 8
1 0
NO
4 1 2 3 -5
YES 4 1 1 2 2 3 3 4 4
大意:给你一个数组,问是否能找到元素和不为 0 的子数组。
思路:只要原数组不全为 0 就肯定 YES 啊,然后不为 0 的元素自己就是一个(只存在一个元素)数组,碰到 0 的元素,就跟下一个不为的 0 的元素合一块作为一个数组就行了
#include
#include
#include
using namespace std;
typedef pair pii;
int n;
int a[110];
pii ans[110];
int main()
{
while(~scanf("%d",&n))
{
int cnt=0,k=0,p=1;
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
if(a[i]!=0)
{
ans[k].first=p;
ans[k++].second=i;
p=i+1;
}
}
if(p!=n+1) // 说明末尾有存在一定数量的 0
ans[k-1].second=n;
if(k==0)
{
puts("NO");
}
else
{
puts("YES");
printf("%d\n",k);
for(int i=0;i
大意:给你一个 4X4 的方格,两个人下棋,三个棋子连起来(横的,竖的,斜的)就获胜。现在该 llya 下棋了,问他落下一个棋子是否能获胜。
思路:出现可能的情况也不是很多,枚举每一种情况判断即可
#include
#include
#include
#include
using namespace std;
char cell[10][10];
bool judge(int px,int py)
{
if(px>=0&&px<4&&py>=0&&py<4)
return 1;
return 0;
}
bool find(int px,int py,char ch)
{
if(judge(px+1,py)&&judge(px+2,py)) // 同一列
{
if(cell[px+1][py]==ch&&cell[px+2][py]==ch)
return 1;
}
if(judge(px-1,py)&&judge(px+1,py))
{
if(cell[px-1][py]==ch&&cell[px+1][py]==ch)
return 1;
}
if(judge(px-2,py)&&judge(px-1,py))
{
if(cell[px-2][py]==ch&&cell[px-1][py]==ch)
return 1;
}
if(judge(px,py+1)&&judge(px,py+2)) // 同一列
{
if(cell[px][py+1]==ch&&cell[px][py+2]==ch)
return 1;
}
if(judge(px,py-1)&&judge(px,py+1))
{
if(cell[px][py-1]==ch&&cell[px][py+1]==ch)
return 1;
}
if(judge(px,py-2)&&judge(px,py-1))
{
if(cell[px][py-2]==ch&&cell[px][py-1]==ch)
return 1;
}
if(judge(px+1,py+1)&&judge(px+2,py+2)) // 对角线(向右斜)
{
if(cell[px+1][py+1]==ch&&cell[px+2][py+2]==ch)
return 1;
}
if(judge(px-1,py-1)&&judge(px+1,py+1))
{
if(cell[px-1][py-1]==ch&&cell[px+1][py+1]==ch)
return 1;
}
if(judge(px-2,py-2)&&judge(px-1,py-1))
{
if(cell[px-2][py-2]==ch&&cell[px-1][py-1]==ch)
return 1;
}
if(judge(px-1,py+1)&&judge(px-2,py+2)) // 对角线(向左斜)
{
if(cell[px-1][py+1]==ch&&cell[px-2][py+2]==ch)
return 1;
}
if(judge(px+1,py-1)&&judge(px-1,py+1))
{
if(cell[px+1][py-1]==ch&&cell[px-1][py+1]==ch)
return 1;
}
if(judge(px+1,py-1)&&judge(px+2,py-2))
{
if(cell[px+1][py-1]==ch&&cell[px+2][py-2]==ch)
return 1;
}
return 0;
}
int main()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
cin>>cell[i][j];
}
int num1=0,num2=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(cell[i][j]=='x')
num1++;
if(cell[i][j]=='o')
num2++;
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(cell[i][j]=='.')
{
if(num1>num2)
{
if(find(i,j,'o'))
{
puts("YES");
return 0;
}
}
else if(num1==num2)
{
if(find(i,j,'x'))
{
puts("YES");
return 0;
}
}
}
}
}
puts("NO");
return 0;
}