为什么当while不满足语句条件的时候依然循环

msy_cat 2011-09-29 08:29:26
#include<iostream>
#include<stack>

using std::cin;
using std::cout;
using std::endl;
using std::stack;

int main()
{
int people[200000];
int N,a,b;

while (cin >> N != 0)
{
stack<int> couple;

for (int i = 0;i < N;i++)
{
cin >> a >> b;
people[a - 1] = people[b - 1 ] = i + 1;
}

for (int j = 0;j < 2 * N ;j++)
{
if (couple.empty())
couple.push(people[j]);
else
{
if(people[j] == couple.top())
couple.pop();
else
couple.push(people[j]);
}
}

if (couple.empty())
cout << "Yes" << endl;
else
cout << "No" << endl;
}

return 0;
system("pause");

}

当我输入0的时候应该是直接停止吧 可是它输出了yes 也就是执行了最后的几行程序
...全文
1227 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pathuang68 的回复:]

引用楼主 msy_cat 的回复:
#include<iostream>
#include<stack>

using std::cin;
using std::cout;
using std::endl;
using std::stack;

int main()
{
int people[200000];
int N,a,b;

while (cin >> N !……
[/Quote]

忘记了格式

#include <iostream>
using namespace std;

int main(void)
{
int n;
while(cin >> n && n != 0) // 这样写就OK了
{
cout << "hello, C++" << endl;
}
return 0;

pathuang68 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用楼主 msy_cat 的回复:]
#include<iostream>
#include<stack>

using std::cin;
using std::cout;
using std::endl;
using std::stack;

int main()
{
int people[200000];
int N,a,b;

while (cin >> N != 0)
……
[/Quote]

楼主可以这么写:
#include <iostream>
using namespace std;

int main(void)
{
int n;
while(cin >> n && n != 0) // 这样写就OK了
{
cout << "hello, C++" << endl;
}
return 0;
aixin1234567890 2011-09-30
  • 打赏
  • 举报
回复
4楼正解
5t4rk 2011-09-30
  • 打赏
  • 举报
回复
while (cin >> N != 0)//这样写是错误的


打开<ISTREAM>头文件,找到类模板basic_istream的定义,摘出这么两个语句来:

typedef basic_istream<_E, _Tr> _Myt;

_Myt& operator>>(......) ......

这说明cin >>的返回值类型就是basic_istream&,可是放到while()中情况又该是怎样的。while()中要求是

布尔表达式,难不成basic_istream&类型可以转换成bool类型?继续查看头文件,发现所有的operator重载函


数都是<< 和>>,没有找到用于类型转换的操作那就只好追溯到父类basic_ios了。

打开头文件<IOS.H>,找到ios的定义,其中有这么一条语句,类型转换函数的定义:

operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }

有这个函数的定义之后,编译器会在需要的情况下将ios类型自动转换为void*类型。因此,在表达式while

(cin >> m >> n)中,括号中的表达式为了匹配bool类型将自动转换为void*类型。如果读入时发生错误返回0,否则返回cin的地址。
jaylong35 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mengmingtao 的回复:]

cin >> N,这个表达式返回的是cin流,而不是N的值。
[/Quote]

“>>”输入操作符返回的是其左操作数,所以cin>>N返回的是cin
这也是为什么支持 cin>>v1>>v2;的原因。

必然的,cin一定不等于0 所以循环依然继续
粉末的沉淀 2011-09-30
  • 打赏
  • 举报
回复
不好偷懒,写成下面这样子嘛
cin>>N;
while (N != 0)
{
……
}
翻越寒武 2011-09-30
  • 打赏
  • 举报
回复
++[Quote=引用 4 楼 bjtbjt 的回复:]
while (cin >> N != 0)//这样写是错误的


打开<ISTREAM>头文件,找到类模板basic_istream的定义,摘出这么两个语句来:

typedef basic_istream<_E, _Tr> _Myt;

_Myt& operator>>(......) ......

这说明cin >>的返回值类型就是basic_istream&……
[/Quote]
粉末的沉淀 2011-09-30
  • 打赏
  • 举报
回复
看来是新手
天亮后说晚安 2011-09-30
  • 打赏
  • 举报
回复
题目做不起,我是来顶个 拿分的
海中水草 2011-09-30
  • 打赏
  • 举报
回复
看看有关"cin"方面的知识和while循环
阿尼小码 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mengmingtao 的回复:]
cin >> N,这个表达式返回的是cin流,而不是N的值。
[/Quote]
++
tzg_dzq 2011-09-30
  • 打赏
  • 举报
回复
有意思!!!!!!!!!!!!!
alfredtofu 2011-09-30
  • 打赏
  • 举报
回复
你C++学的不好!!输入流的级联都不知道!!btw..你是?
haican963 2011-09-30
  • 打赏
  • 举报
回复
cin>>n返回的是一个流。
clannadgirl 2011-09-30
  • 打赏
  • 举报
回复
while(cin>>N,N!=0)
赵4老师 2011-09-30
  • 打赏
  • 举报
回复
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
whlie (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。
冼鸿文 2011-09-30
  • 打赏
  • 举报
回复
学习了
jixingzhong 2011-09-29
  • 打赏
  • 举报
回复
while (cin>>N)
...

可以考虑使用这种方式。
mengmingtao 2011-09-29
  • 打赏
  • 举报
回复
cin >> N,这个表达式返回的是cin流,而不是N的值。

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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