标题:我的程序怎么了?
只看楼主
DoubleFly
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-2-8
 问题点数:0 回复次数:2 
我的程序怎么了?

#include <iostream>
int main()
{
using namespace std;
unsigned short small;
unsigned long large;
unsigned long skip;
unsigned long target;
const unsigned short MAXSMALL=65536;
cout<<"enter a smll number:";
cin>>small;
cout<<"enter a large number:";
cin>>large;
cout<<"enter a skip number:";
cin>>skip;
cout<<"enter a target number:";
cin>>target;
cout<<"\n";
while (small<large && large>0 && small<MAXSMALL)
{
small++;
if (small%skip==0)
{
cout<<"skipping on"<<small<<endl;
continue;
}
if (large==target)
{
cout<<"target reached!";
break;
}
large-=2;
}
cout<<"\nsmall:"<<small<<"large:"<<large<<endl;
return 0;
}

我输入了2 ,20 ,4 ,6这四个数字,按道理说应该显示
Skipping on 4
Skipping on 8
Small 10 large 8
为什么我的输出是small:2 large:20
为什么我前面的条件语句没起作用?

搜索更多相关主题的帖子: include number target 
2007-02-08 16:21
slowlybear
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2006-10-18
得分:0 
第9行:
const unsigned short MAXSMALL=65536;

你用了short,这个是不支持到65536的,去掉以后程序结果正确.

2007-02-08 19:59
plusone
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-8-7
得分:0 

我帮你改了下.
#include <iostream.h>
#define MAXSMALL 65536

int main()
{
//using namespace std;
unsigned short small;
unsigned long large;
unsigned long skip;
unsigned long target;
//const unsigned short MAXSMALL=65536;
cout<<"enter a smll number:";
cin>>small;
cout<<"enter a large number:";
cin>>large;
cout<<"enter a skip number:";
cin>>skip;
cout<<"enter a target number:";
cin>>target;
cout<<"\n";
while (small<large && large>0 && small<MAXSMALL)
{
small++;
if (small%skip==0)
{
cout<<"skipping on"<<small<<endl;
continue;
}
if (large==target)
{
cout<<"target reached!";
break;
}
large-=2;
}
cout<<"\nsmall:"<<small<<"large:"<<large<<endl;
return 0;
}


2007-02-08 20:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-120240-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.098006 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved