c++while

来源:百度知道 编辑:UC知道 时间:2024/06/15 18:57:20
#include<iostream.h>
int main()
{
int n,m,g,t,k;

while(cin>>n>>m,n,m)
{

k=m%2;
if(k!=0)
cout<<"Error"<<endl;
else
{
t=m/2-n;
g=2*n-m/2;
if(t>0&&g>0)
cout<<t<<g<<endl;
else
cout<<"Error"<<endl;
}
这是一个关于求鸽子和兔子的问题n,m分别为头合脚。g为鸽子t为兔子。编译的时候无错误运行的结果错了。请哪位大哥大姐看看帮帮改下。while语句是不是与问题。
调试运行后会出现如下文字,可以运行结果是错的:Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\Windows\System32\kernel32.dll', no matching symbolic information found.
The thread 0xBE4 has exited with code 4391088 (0x4300B0).
The program 'D:\e\新建文件夹\vc\MSDev98\MyProjects\1000\Debug\1000.exe' has exited with code 4391088 (0

此题不能使用while循环,如果使用while循环的话,将会造成死循环的,程序修改后如下:
#include<iostream.h>
void main()
{
int n,m,g,t,k;
cin>>n>>m;
if(n>0&&m>0)
{

k=m%2;
if(k!=0)
cout<<"Error"<<endl;
else
{
t=m/2-n;
g=2*n-m/2;
if(t>0&&g>0)
cout<<t<<endl<<g<<endl;
else
cout<<"Error"<<endl;
}
}
else
cout<<"Error"<<endl;
}
但是如果想使用while循环的话也可以的,程序修改如下:
#include<iostream.h>
void main()
{
int n,m,g,t,k;
cin>>n>>m;
while(n>0&&m>0)
{

k=m%2;
if(k!=0)
cout<<"Error"<<endl;
else
{
t=m/2-n;
g=2*n-m/2;
if(t>0&&g>0)
{
cout<<t<<endl<<g<<endl;
break;
}
else
{
cout<<&qu