C++栈问题求助各位大神,急用,谢谢啦!!!

Yeqin_Zheng 2012-12-02 04:11:58
Description
N couples are standing in a circle, numbered consecutively clockwise from 1 to 2N. Husband and wife do not always stand together. We remove the couples who stand together until the circle is empty or we can't remove a couple any more. Can we remove all the couples out of the circle?
Input
There are multiple test cases. The first line contains an integer T, indicating the number of test cases. In each case, the first line is an integer N(1 <= N <= 100000)----the number of couples. In the following N lines, each line contains two integers ---- the numbers of each couple.
Output
Output "Yes" if we can remove all the couples out of the circle. Otherwise, output "No".

Sample Input
2
4
1 4
2 3
5 6
7 8
2
1 3
2 4
Sample Output
Yes
No

我的代码:

#include<iostream>
#include<stack>
using namespace std;
int main()
{
int n,i,c,k,j,a[100000][2],n2;
stack<int>couple;
cin>>c;
for (i=0;i<c;i++)
{
while(!couple.empty())
{
couple.pop();
}
cin>>n;
for (j=0;j<n;j++)
cin>>a[j][1]>>a[j][2];
couple.push(1);
n2=n*2;
for (j=2;j<=n2;j++)
{
for (k=0;k<n;k++)
{
if ((couple.top()==a[k][1]&&j==a[k][2])||(couple.top()==a[k][2]&&j==a[k][1]))
{
couple.pop();
continue;
}
else if (k==n)
couple.push(j);
}
}
if (couple.empty())
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}


不知道问什麽总是运行出错;
...全文
161 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
laowang2 2012-12-03
  • 打赏
  • 举报
回复
E文不好啊
Yeqin_Zheng 2012-12-02
  • 打赏
  • 举报
回复
引用 5 楼 dataxdata 的回复:
比如,case1中,各个couple如下 1 (1,4) 2 (2,3) 3 (5,6) 4 (7,8) 那么这个circle中,各个couple的分布应该是这样的: 1,2,2,1,3,3,4,4 用这个序列进行堆栈操作就可以了,拿最初的1,4,2,3,5,6,7,8这个序列是不行的
瞭解,謝謝啦
dataxdata 2012-12-02
  • 打赏
  • 举报
回复
比如,case1中,各个couple如下 1 (1,4) 2 (2,3) 3 (5,6) 4 (7,8) 那么这个circle中,各个couple的分布应该是这样的: 1,2,2,1,3,3,4,4 用这个序列进行堆栈操作就可以了,拿最初的1,4,2,3,5,6,7,8这个序列是不行的
Yeqin_Zheng 2012-12-02
  • 打赏
  • 举报
回复
還是木有聽懂。。。
dataxdata 2012-12-02
  • 打赏
  • 举报
回复
以case 1为例: 首先每个couple对应一个tag,比如couple(1,4)的tag是1,couple(2,3)的tag是2,以此类推; 然后用一个数组,标识出每个couple的tag所在的位置,哪个这个数组的值如下: 1,2,2,1,3,3,4,4 然后再把这个tag数组依次push到栈中,如果栈顶的两个tag相同,就都pop出来; 当所有tag都push进栈后,如果这时栈的长度为0,就说明所有的couple都被remove掉了,否则,就表明有无法remove的
Yeqin_Zheng 2012-12-02
  • 打赏
  • 举报
回复
引用 1 楼 dataxdata 的回复:
挺有意思的题,就跟祖玛一样
那你知道我哪裡錯了嗎???
dataxdata 2012-12-02
  • 打赏
  • 举报
回复
挺有意思的题,就跟祖玛一样

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧